The following code is supposed to wait for the delay ( 5 seconds ) and then spawn an object. It just waits for the delay and then goes berserk spawning them every frame. I have looked over the coroutines reference many times but can’t seem to figure out what is wrong with this :L
var Object1 : GameObject;
var chancePercentage : float = 100.0;
var chance : float = 1.0;
var delay : float = 5.0;
private var spawn : boolean = true;
function Update ()
{
Spawn();
}
function Spawn()
{
var rand1 = Random.Range(0.0, chance);
var rand2 = Random.Range(1.0, 8.0);
var spawnPos = Vector3(transform.position.x, transform.position.y, rand2);
while(spawn == true)
{
yield WaitForSeconds(delay);
if(rand1 <= chance)
{
Instantiate(Object1, spawnPos, Object1.transform.localRotation);
}
}
}
Any help is greatly appreciated.
Thanks,
Dusk