x


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);
    }
}
}
more ▼

asked Apr 04 '10 at 09:26 AM

dreal gravatar image

dreal
45 6 6 12

how about making the level end when they all die?

Apr 22 '10 at 05:30 PM Not showing my name
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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")

more ▼

answered Apr 04 '10 at 04:18 PM

duck gravatar image

duck ♦♦
41k 92 148 415

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Apr 04 '10 at 08:58 PM

Eric5h5 gravatar image

Eric5h5
80.2k 41 132 519

Thanks alot, But how would i destroy the ramaining zombies that have already spawned after the instantiate function is shut off??

Apr 05 '10 at 02:34 AM dreal

@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);

Apr 05 '10 at 04:34 AM Eric5h5
(comments are locked)
10|3000 characters needed characters left

I think when the level finishes you should have a BroadcastMessage that disables the zombie spawner.

more ▼

answered Apr 04 '10 at 08:14 PM

Peter G gravatar image

Peter G
15k 16 44 136

ah now THAT'S and idea.

Apr 22 '10 at 05:34 PM Not showing my name
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2086
x1672
x1043
x764
x275

asked: Apr 04 '10 at 09:26 AM

Seen: 1748 times

Last Updated: Apr 05 '10 at 02:49 AM