WaitForSeconds syntax problem

Hi again…

I’m kinda stuck on how to use the WaitForSeconds. The idea here is I want to make sure that the animation plays out before making the object inactive. My script looks like this:

var dataflows : GameObject;
var topdoor: GameObject;
var boxbottom: GameObject;
var boxanimation: GameObject;

function Update () {
if (Input.GetKeyDown ("m")) {
boxanimation.animation.CrossFade("Close");
yield WaitForSeconds (2);
topdoor.active = false;
boxbottom.active = false;
dataflows.SetActiveRecursively(true);
}


if (Input.GetKeyDown ("n")) {
topdoor.active = true;
boxbottom.active = false;
boxanimation.animation.CrossFade("Open");

dataflows.SetActiveRecursively(false);
}
}

But no go. (Update() can not be coroutine.). Where am I wrong on that?

You can’t use yield in Update. Update runs every frame so that wouldn’t be a good idea. :wink: Make the result of pressing a key into a separate function.

–Eric

Thanks much. I actually lifted a piece of code you posted as a help for someone else and frankensteined it together. So thanks twice! :smile: