- Home /
What is the best method for setting up a variable wait time in a coroutine?
There's a point in a coroutine where I have the typical
yield return new WaitForSeconds(variableTime);
but if something happens in the meantime, I would like to force the coroutine to stop waiting and move on to the next step. I've been thinking of doing a while loop, checking if a boolean has changed and how much time has passed, but not sure if this is the most efficient way if I can instead just change the variableTime to 0 and/or just have it move on instead of checking for more.
Answer by Polymo · Sep 12, 2015 at 02:06 PM
float timePassed = 0;
while (YourCondition != true & timePassed < YourDelay)
{
timePassed+=Time.deltaTime;
yield return new WaitForEndOfFrame();
}
should work, i think
This is what I ended up doing, but I still don't know if it's optimal. Glad to see we had the same idea, though.
Your answer
Follow this Question
Related Questions
Coroutine doesn't work when called from another method. 3 Answers
Rotate an object using Quaternion.RotateTowards() inside a Coroutine? 1 Answer
Can't get past WaitForSeconds in my coroutine 1 Answer
Waypoint / Yield help 1 Answer
Coroutine not running after yield return new WaitForSeconds 3 Answers