- Home /
Destroying all enemy characters after the level is over
Hey, I'm trying to stop instantiating my enemy character when the level ends which last for 4 seconds then goes to the next stage. I don't want enemies to still be attacking when the level is done. Any Help?
Thanks alot, But how would i destroy the ramaining zombies that have already spawned after the instantiate function is shut off??
var wakeUpZombie : GameObject; var wakeUpZombie1 : GameObject;
var wakeUpZombie3 : GameObject; var wakeUpZombie4 : GameObject; var wakeUpOldZombie1 : GameObject; var wakeUpOldZombie2 : GameObject; //var wakeUpOldZombie3 : GameObject; var stopZombies : EnemyAI;
function Start() {
reSpawn(); reSpawn2(); reSpawn3(); reSpawn4(); reSpawnOldZombie1(); reSpawnOldZombie2(); //reSpawnOldZombie3(); }
//This is for My Zombie.... function reSpawn(){ while(true){ yield new WaitForSeconds(15); wakeUpZombie.Instantiate(wakeUpZombie); if(stopZombies.count == 13){ Destroy(wakeUpZombie); } } }
function reSpawn2(){ while(true){ yield new WaitForSeconds(50); wakeUpZombie.Instantiate(wakeUpZombie1);
if(stopZombies.count == 13){ Destroy(wakeUpZombie1); } } }
function reSpawn3(){ while(true){ yield new WaitForSeconds(15); wakeUpZombie.Instantiate(wakeUpZombie3);
if(stopZombies.count == 13){ Destroy(wakeUpZombie3); } } }
function reSpawn4(){ while(true){ yield new WaitForSeconds(30); wakeUpZombie.Instantiate(wakeUpZombie4);
if(stopZombies.count == 13){ Destroy(wakeUpZombie4); } } }
how about making the level end when they all die?
Answer by duck · Apr 04, 2010 at 04:18 PM
I think what you probably need to do here is to have a boolean variable which determines whether the game is within the 4-second "finishing" state.
Then, you could replace all your
while(true)
statements, with
while(!finishing)
(the ! means "not")
Answer by Eric5h5 · Apr 04, 2010 at 08:58 PM
Considering that your code is doing the same thing over and over with just a couple of minor variations, it would be far shorter and easier to manage if you use an array and a function that takes a couple of parameters:
var wakeUpZombie : GameObject[];
function Start() { Respawn(0, 15); Respawn(1, 50); Respawn(2, 15); Respawn(3, 30); }
function Respawn (zombieNumber : int, delay : float) { while (true) { yield WaitForSeconds(delay); if (EnemyAI.count < 13) { Instantiate(wakeUpZombie[zombieNumber]); } } }
function LevelEnd () { StopAllCoroutines(); }
You can call the LevelEnd function from somewhere when the level ends, and that will make all the Respawn functions stop.
Thanks alot, But how would i destroy the ramaining zombies that have already spawned after the instantiate function is shut off??
@dreal: easiest way is to give them all a tag, called "Zombie" I guess, then do var zombies = GameObject.FindGameObjectsWithTag("Zombie"); for (zombie in zombies) Destroy (zombie);
Answer by Peter G · Apr 04, 2010 at 08:14 PM
I think when the level finishes you should have a BroadcastMessage that disables the zombie spawner.
Thanks for Giving us Such a Great Information telldunkin
Answer by Llama_w_2Ls · Aug 19, 2020 at 08:41 AM
Well in your spawner you could parent all zombies, when theyre instantiated, to an empty GO, then, when the level is complete, run some code to destroy all GOs inside that empty. For example:
void DestroyAllEnemies()
{
foreach(Transform child in EmptyParentGO)
{
Destroy(child.gameObject);
}
}
(Assuming that all enemies are children of the gameobject EmptyParentGO)