- Home /
Coroutines, WaitForSeconds with function Update?
I am having trouble with a coroutine. The bottom script is not my actual script, but a watered down version because the actual script is very long and boring. Basically I have an if/else statement and the else part I want my update to do something, do the function with the coroutine, and then continue back down the line. The problem I face is that Once it gets to the yield WaitForSeconds part is skips the rest of the coroutine and goes back to the Update. Is this because I am making the function mycoroutine yield back to update or what? How can I make a pause in my line of commands. Thank you for the help. I've been trying really hard and looking for already posted coroutine questions but they haven't been much help.
function Update() { if(whatever) { Do something; } else { Debug.Log("Reached else"); MyCoroutine(); Debug.Log("Done with else"); }
}
function MyCoroutine() { Debug.Log("Coroutine started"); yield WaitForSeconds (5); Debug.Log("Coroutine finished"); }
Answer by Eric5h5 · Jul 27, 2010 at 06:20 AM
You can't use coroutines like that from Update. You can start coroutines in Update, but you can't yield on them (since using yield can only be done in a coroutine). Update happens once every frame regardless, and will never pause for anything. When scheduling a sequence of events with coroutines, it's much simpler just to avoid using Update entirely, since it's not appropriate for that kind of code.
Thank you for your response. It is doing the same thing even when using StartCoroutine or not using it at all. It goes into the my coroutine function and executes everything up until the yield to make it wait for two seconds and then it gets skipped over and goes back to the function Update. What should I do to get the same effect of making it wait for two seconds? What can I do?
@Panamamigo, StartCoroutine is unnecessary in JS. As I said, you just can't use Update for this sort of thing, at all. Remove the Update function, and stick with coroutines.
So basically I would make a coroutine called $$anonymous$$yUpdate() or something and that would run every frame unless I told it not to or to yield?
@Panamamigo, it depends on what your code is doing, but that's a possibility. For example, this code checks every frame for input, and once it gets some, then it performs another routine for a while, then goes back to checking input: http://www.unifycommunity.com/wiki/index.php?title=Grid$$anonymous$$ove
Well what I'd like to do for the code in the original question I had asked is to change the function Update() to a coroutine so it wouldn't matter if I made it yield or anything. I just don't know how to make a coroutine that acts just like function Update and haven't been able to find anything on the web either.
Answer by qJake · Jul 27, 2010 at 05:17 AM
You aren't starting the coroutine properly. You need to use StartCoroutine("MyCoroutine");
Apart from Yield Waitforseconds. Is that any other ways to pause the game????