x


Spawning trouble

Hello, I'm having trouble with a script for Mass Spawning to create a swarm of enemies in a side scrolling shooter game. It respawns but makes more than the intended number. Also they are too closely summoned, so I wanted to add a Wait function before continuing the loop but the code didn't seem to work. Does anyone have suggestions?

//What enemy are we spawning? var Enemy: GameObject; //Add regular timer here and rest timer function Update () { //Is the enemy already Out? //Change to timer in future? if (GameObject.FindWithTag("Enemy") == false){ Wait 0 seconds before triggering the Mass Spawn function Invoke("MassSpawn", 0); } else { CancelInvoke(); } }

function MassSpawn(){ //Set up a loop to add until i =5, loop 5 times, make 5 respawns for(var i: int = 0; i < 5; i++ ){ //WaitForSeconds(1); Instantiate(Enemy, transform.position, Quaternion.identity);

 }

}

more ▼

asked Jul 31, 2010 at 02:08 PM

Persona gravatar image

Persona
293 214 175 185

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

1 answer: sort voted first

You want to call the function normally instead of using Invoke if you want to use it as a coroutine, and then use yield before WaitForSeconds. You also need some sort of check to make sure you're not running the coroutine multiple times. e.g.:

//What enemy are we spawning? var Enemy: GameObject; var spawning = false;

//Add regular timer here and rest timer function Update () { //Is the enemy already Out? //Change to timer in future? if (!spawning && GameObject.FindWithTag("Enemy") == null){ MassSpawn(); } }

function MassSpawn() { if (spawning) yield break; spawning = true; //Set up a loop to add until i =5, loop 5 times, make 5 respawns for(var i: int = 0; i < 5; i++ ){ yield WaitForSeconds(1); //this should work now Instantiate(Enemy, transform.position, Quaternion.identity); } spawning = false; }

more ▼

answered Jul 31, 2010 at 02:18 PM

Mike 3 gravatar image

Mike 3
33.3k 21 93 299

if (spawning) yield break; Causes an error to appear but remove that and it works. Thanks.

Jul 31, 2010 at 06:10 PM Persona

Oh, sorry, bit of c#there. It was more of a precaution anyway, as it was already checked outside the function.

Jul 31, 2010 at 06:31 PM Mike 3
(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:

x467
x285

asked: Jul 31, 2010 at 02:08 PM

Seen: 1208 times

Last Updated: Jul 31, 2010 at 02:08 PM