- Home /
interrupt WaitForSeconds() ?
Hey Unity Answers! Is there a way to interrupt a function that is currently waiting at the "WaitForSeconds()" step?
My situations is as follows: I have boxes that move every 5 seconds, so, I have a WaitForSeconds(5) in this function that I'm using, and it works great. However, when the next level starts, I need to destroy these boxes, load the next level and new boxes, and start moving them around every 5 seconds again.
When this happens, I get strange results in the timing that the boxes move. Sorta like, 5 seconds, move, 2 seconds move... My current thoughts are that since the player can go to the next level at any time, even if my function is in the waiting period, the timing in my game is getting shifted. Because, when I load the next level, the timing is all messed up, and it changes every 5 seconds, AND every 5 seconds from when the next level started.
So, what I'm hoping to do is, have some sort of exit from the WaitForSeconds, that if a bool is set to true, that it jumps out of the function. Is there something out there that I am just totally overlooking?
Thanks in advance!
Answer by Eric5h5 · Jan 03, 2011 at 04:12 AM
Make your own WaitForSeconds routine that is interruptible:
private var cancelWait = false;
function Wait (waitTime : float) { var t = 0.0; while (t <= waitTime && !cancelWait) { t += Time.deltaTime; yield; } }
$$anonymous$$y compiler requires yield return null;
ins$$anonymous$$d of yield;
.
Your answer
Follow this Question
Related Questions
Csharp: how to make script wait for x seconds 3 Answers
About WaitForSeconds 1 Answer
Trying to make WaitForSeconds work 3 Answers
Coroutine doesn't work when called from another method. 3 Answers
Looping a Script 2 Answers