- Home /
Pause coroutine with infinite loop
I use infinite loop in coroutine to simulate code execution similar to FixedUpdate. Here example:
IEnumerator My_Coroutine()
{
while (true)
{
Do_Something;
yield return new WaitForFixedUpdate();
}
}
But how i can pause this coroutine when i need to pause game (for example) with main menu call? StopCoroutine disable coroutine, not pause it. So what you can advise for this situation?
Hi @Inok, why not just make an if statement to test for game pause?, if for example pause condition is met then don't call "Do_Something()", why not try somthing like that:
bool pause = false;
IEnumerator $$anonymous$$y_Coroutine()
{
while (true)
{
if (!pause)
Do_Something;
yield return new WaitForFixedUpdate();
}
}
Answer by HarrisonJH · Jan 11, 2015 at 09:20 AM
if (Input.GetKeyDown (KeyCode.Escape))
Time.timeScale = (Time.timeScale == 0) ? 1 : 0;
Try this
this variant unacceptable because coroutine not depend on Update or FixedUpdate, TimeScale affect only them.
@Inok Did you try? I tested and it worked. As I know, the while loop only runs one time each frame.
Your answer
Follow this Question
Related Questions
Audio problem 1 Answer
Pause a coroutine not working 1 Answer
Pause a Coroutine from restarting on scene change? 1 Answer