- Home /
Do Coroutines HAVE to be stopped manually?
Consider the following IEnumerator function:
IEnumerator MyCoroutine()
{
while (true)
{
if (something)
{
//do something
break;
}
else
yield return 0;
}
}
When something happens, the code exits the loop, and the function reaches an end.
But does the coroutine (or should the coroutine) be manually stopped as well with
StopCoroutine("MyCoroutine");
Or is it fine to just let the function end like normal functions?
Answer by neonblitzer · May 28, 2014 at 02:50 PM
You don't need to end them manually. Coroutines are normal functions that are iterated and executed by the Unity engine, halting the execution for some time (one frame by default) when the yield
statement is entered. When there is nothing more to iterate, Unity stops executing the function. You won't free any resources or anything like that by terminating the coroutine manually. :)
EDIT: Wasn't fast enough x)
Thanks neonblitzer, I think I like your answer a little more than Landern's ;)
Answer by Landern · May 28, 2014 at 02:47 PM
You can let it run, StopCoroutine is useful to stop coroutines that may be long lasting or looping and require a manual stop.