- Home /
The question is answered, right answer was accepted
[SOLVED] Is StopAllCoroutines stopsing itself?
Hello there!
I have this simple question. If you execute this: what will happen? Because i debbug it and it seems it continue executing, but it nere restarts, can someone explain me how it works a little better?
IEnumerator Deambular()
{
StopAllCoroutines();
if (something) // THINGS A
else
{
// THINGS B
yield return new WaitForSeconds(3));
StartCoroutine(Deambular());
}
}
I wil never restart itself due that StopAllCoroutines, right=? Or What ?
Thanks in advance
Answer by TreyH · Oct 21, 2019 at 02:20 PM
Basically StopAllCoroutines
will kill any instructions that are suspended by a yield
instruction. In the simplest case:
IEnumerator Deambular ()
{
StopAllCoroutines ();
Debug.Log("BEFORE YIELD");
yield return null;
Debug.Log("AFTER YIELD");
}
will only print "BEFORE YIELD"
.
Thanks for answer, so what i say in the other answer comment is true?
Yeah, both will print since your StartCoroutine
call adds a new coroutine to your $$anonymous$$onoBehaviour right after you clear the others. In the same way, doing that from within the Deambular()
function would cause a stack overflow.
Just onle last qeustion. I posted it here
https://answers.unity.com/questions/1673561/stopallcoroutines-stops-coroutines-in-other-script.html
Thanks!
Answer by misher · Oct 21, 2019 at 02:13 PM
You should not call StapAllCoroutines()
inside a coroutine. Do it before, it will stop all coroutines, theb if you start another one it will not be affected. If you want to stop a coroutine from inside, you can use yield break
instead
I see, then, if do this this:
StopAllCoroutines ();
StartCoroutine( Deambular () );
IEnumerator Deambular ()
{
Debug.Log("BEFORE YIELD");
yield return null;
Debug.Log("AFTER YIELD");
}
will print both sentences?
Thanks for your time guys!
Just onle last qeustion. I posted it here
https://answers.unity.com/questions/1673561/stopallcoroutines-stops-coroutines-in-other-script.html
Thanks!
Follow this Question
Related Questions
How to stop coroutines or functions 2 Answers
Coroutine exiting unexpected 1 Answer
How to Stop C# Coroutine 2 Answers
StopAllCoroutines() stops coroutines in other scripts? -- Other behaviours ?? 1 Answer
What will StopCoroutine exactly do? 2 Answers