Hello all! I have a spawning system that checks the maximum spawn limit by randomising between two locations and if its below it, it will spawn more zombies.
At the moment it all works good except and the start of the game as it starts spawning it spawns the zombies at the random locations however all at once which makes them explode (which does look pretty cool) but its not what I want. So I inserted a delay using
yield WaitForSeconds(5);
however it only seems to run the delay code once then never again so it waits for 5 seconds, then spawns the exploding zombies. This is my code;
#pragma strict
//Integers
static var MaxAI = 5;
var EnemysSpawned = 0;
static var EnemysKilled = 0;
var spawnLocation = 1;
//boolean
var spawnForLevel = true;
//Transforms
var spawnLocationR : Transform;
var spawnLocationL : Transform;
var locationToSpawn : Transform;
//Enemys
var Zombie : Transform;
function Update ()
{
if (spawnForLevel == true)
{
if (EnemysSpawned < Spawning_Rate.MaxAI)
{
randomGens();
Delay();
var spawnZombie = Instantiate (Zombie, locationToSpawn.position, Quaternion.identity);
EnemysSpawned += 1;
}
if (EnemysSpawned == Spawning_Rate.MaxAI)
{
spawnForLevel = false;
}
}
}
//This is the delay code, it only seems to run this once then skip it every other time
function Delay()
{
yield WaitForSeconds(5);
}
//Randomise Code
function randomGens ()
{
//Position
var randPos = Random.Range(0, 1);
spawnLocation = randPos;
switch (EnemysSpawned)
{
case 0:
locationToSpawn = spawnLocationR;
break;
case 1:
locationToSpawn = spawnLocationL;
break;
}
}
I have also tried using LateFunction but no luck how can I make it run the code more than once? thank you!