- Home /
When there is a while-loop in a Coroutine, if I have a "yield" within the while-loop, does that mean the Coroutine will be re-entered within the while loop?
Say I have something like:
function Update ()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
StartCoroutine("myCoroutine");
}
if (Input.GetKeyDown("p"))
{
StopCoroutine("myCoroutine");
}
}
function myCoroutine ()
{
var t : float = 0;
while (t <= 1.0)
{
t += 0.1;
yield;
}
}
Does that mean the only part of the coroutine that gets repeated is the part within the while loop, and the t variable will not be reset to 0 (unless I press (Input.GetKeyDown("P") at some point)?? Much thanks!
Answer by AyAMrau · Sep 05, 2014 at 05:14 PM
Yes, basically after you yield, the coroutine will restart on the next line after the yield. In your case it's the last statement in the loop, so it will go back to the condition part of the loop.
The t variable will hold the value it had before the yield.
Your answer
![](https://koobas.hobune.stream/wayback/20220613162356im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
While loop out of control...maybe? I'm not sure. Trying to track Coroutines progress. 1 Answer
If the While Condition of a Coroutine is false is the coroutine halted? 2 Answers
start coroutine everytime 2 Answers
Instantiated object from asset bundle is null 0 Answers
Coroutine only fires once instead of looping until stopped. 2 Answers