- Home /
The question is answered, right answer was accepted
yield WaitForSeconds don't work
Hi, yields WaitForSeconds in this code is not working and I don´t know why, someone can help me?
var clock_b1 : Texture2D[];
var clock_b : GameObject;
var i = 0;
function PlayAnim () {
if(i < 11 ){
i++;
clock_b.guiTexture.texture = clock_b1[i];
yield WaitForSeconds (1);
}else{
i=0;
}
}
have you tried invoke?
like:
void function(){
invoke("FunctionName", 1f);
}
FunctionName(){
Debug.Log("asdf");
}
it's much easyer to understand at least for me
Yes, I invoke the function PlayAnim in the update function,
all the code are working except the yield
It is too working. Every frame, PlayAnim() will be called and i
will be incremented by 1. The timer will reach 11 in 11 frames. One second later, all the code after yield WaitForSeconds (1);
will be executed, finishing in 1 second and 11 frames. There is no code after the yield so there you go.
yield WaitForSeconds
makes the function it is in wait, but it does not prevent that function from being called again multiple times, does not prevent Update from being called every frame, and does not make the entire game wait. That is its intended behavior and it is much more useful that way.
There is not enough of your logic/script here to find your problem. In particular we need to see where/how "PlayAnim" is called. The typical problem is folks calling functions with a 'yield' form Update().
Answer by iwaldrop · Dec 20, 2013 at 05:18 PM
Don't call it from update. Don't make it a coroutine, and instead use invoke repeating.