I am supposing that your enemy
GameObject is inside the scene holding your EnemyTesting
MonoBehaviour instance (correct me if I'm wrong).
If this is the case, you cannot instantiate a gameObject that is destroyed.
As @derHugo pointed out, you should not use Destroy and Instantiate for your use case. It would be better to set inactive the enemy
GameObject, move it to the position that you want, an (re)set it active. It will look like the enemy respawned.
If you want to dig the subject later, look at the object pooling game optimization pattern.
Otherwise, if you still want to use Instantiate for respawning, I would create a prefab of your enemy
GameObject. The enemy
GameObject referenced in the EnemyTesting
field (in the Inspector view), would be your prefab from the project hierarchy instead of a GameObject inside the scene.
This way, you would be able to instantiate an enemy
GameObject as many times as you want (and use it in other scenes!). Don't forget to hold a reference to the instantiated enemy
GameObject so you can know which one you want to destroy. It would looke like this :
enemy = Instantiate(enemyPrefab, transform.position, transform.rotation);
You can replace transform
with the transform of your choice, for example the transform of an empty enemyRespawnPoint
GameObject in your Scene.
Do you see any error in the console ?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…