spawn 1 enemy and once destroyed spawn another=
new to coding and hoping someone could help I'm trying to spawn a enemy , then once destroy another enemy will be spawned..and so on this is what i got so far
public GameObject spawnee; public float spawnTime; public float spawnDelay; int maxEnemies = 1; public static int enemyCounter = 0; // Use this for initialization void Start() { InvokeRepeating("SpawnObject", spawnTime, spawnDelay); }
public void SpawnObject() { if (enemyCounter < maxEnemies) { Instantiate(spawnee, transform.position, transform.rotation); enemyCounter++;
}
}
Answer by UncleanWizard · Sep 13, 2021 at 05:27 AM
You can use the OnDestroy method which is called before a game object is destroyed.
Answer by JustAbhi · Sep 13, 2021 at 05:43 AM
@angelo813 What you have done is spawn gameobjects after same time so there might be a delay in spawning the gameobject.
A better way would be to spawn enemies from the enemy that is currently destroyed. Use the OnDestroy()
method to detect when the enemy is actually destroyed(Gameobject) and then call a function to Spawn(Instantiate) as many enemies as you want.
Hope this helps.
Your answer
![](https://koobas.hobune.stream/wayback/20220613042946im_/https://answers.unity.com/themes/thub/images/avi.jpg)