Hey guys,
I have a script here that is supposed to spawn enemies after a randomly generated amount of seconds, but it only works once. Here is my code:
#pragma strict
var zombiePrefab : GameObject; //Zombie prefab to spawn
var spawnController : GameObject; //Main controller to dictate how many to spawn
var player : GameObject; //player
var zombiesSpawned : int; //keeps track of how many zombies spawned to report back to controller
var waitTime : int; //how long to wait until the next zombie is spawned
var waitMin : int; // minimum time to wait
var waitMax : int; //maximum time to wait
var spawn : boolean; // tells to spawn or not
function Start () { //what happens at start
waitMin = 3; //
waitMax = 10;// ^ Sets initial wait times
waitTime = Random.Range(waitMin,waitMax); //Chooses a random number inbetween min and max to set for waittime
spawn = true; // Tells spawner to spawn
}
function Update () { //constantly happening
if(spawn) //if the spawn variable is true
{
Spawn(); //call function Spawn
}
}
function SetSpawn() { // resets the spawn variable after spawning
if(spawn == false) // if spawn variable is false (happens after every spawn)
{
print("SetSpawn");
yield WaitForSeconds(waitTime);
print("Waited");
spawn = true; // makes it spawn again
}
}
function NewWaitTime() { //resets the amount of time to wait to a random between min and max
waitTime = Random.Range(waitMin, waitMax); //sets waittime
}
function Spawn() { //actually spawns Zombies
if(spawnController.GetComponent(SpawnTimer).roundDone == false)
{
Instantiate(zombiePrefab, transform.position, transform.rotation); //spawns zombie at location of spawner
zombiesSpawned +=1; //adds to spawned count to keep track to send later
NewWaitTime(); // resets wait time
spawn = false; //sets spawn to false (to make you wait until you spawn another zombie)
SetSpawn(); //Runs setspawn to restart the cycle
}
}
What is supposed to happen is it spawns an enemy, then generates a new number and waits for that many seconds, and then spawns another one. I dont understand why its not working, but it spawns one, and then the spawn variable just stays true.