WaitForSeconds doesn t work?

Hi there… I would like to start the animaton after 5 seconds, so I added “yield WaitForSeconds (5)”, but If I add that line, the animation doesn’t start anymore… why?

 var Animazione = "Vagone";
var on : boolean;
function Update () {
if (Input.GetKey(KeyCode.Q)) {
on = !on;
if (!on){
yield WaitForSeconds (5);
GetComponent.<Animation>()[Animazione].speed = 0.5;
GetComponent.<Animation>().Play(Animazione);
}
if (on) {
}
}
}

That’s not the right way to call it. See the official example.

1 Like

Simply change:

yield WaitForSeconds

with:

yield return new WaitForSeconds

I changed It, but “yield return new WaitForSeconds (5);” give me the error: ‘;’ expected. Insert a semicolon at the end.

So I cannot use this in my script, because It can be used only in coroutines?

You can’t use yield in Update(), but you can start a coroutine that uses it from Update(), like in AngryAnt’s example here: http://forum.unity3d.com/threads/waiting-for-seconds-in-update.19347/

Thank you :slight_smile: