- Home /
Question by
ElfElix · Sep 14, 2014 at 10:09 AM ·
2dyieldwhileyield waitforseconds
How do I fix yield not working in while loop?
I get no console errors, but all of them isntantiate at the same time, so they are all one unit, and I want a delay between their spawn. (they are enemies traveling a path)
#pragma strict
// Instantiate a rigidbody then set the velocity
var projectile : Transform;
var cnt : int = 0;
function Update () {
if (buttonFeatures.started) {
while (cnt < 4) {
// Instantiate the projectile at the position and rotation of this transform
wait();
var clone : Transform;
clone = Instantiate(projectile, transform.position, transform.rotation);
cnt++;
}
}
}
function wait(){
yield WaitForSeconds (10);
}
Comment
Best Answer
Answer by alishka · Sep 14, 2014 at 11:01 AM
Hello !
The problem is that the coroutine is within the while loop. The loop will start 4 wait()
coroutines, and four of them will then wait ten seconds. For you to see how this works, try adding a print("wait complete");
line in the wait()
function after the yield; you will get 4 "wait complete" messages in your console.
Cheers !