Hello guys, I have a simple question: does a coroutine terminate itself once it reaches the end of its code block?
I know I know… this is probably a very stupid question, but I just wanna make sure Unity doesn’t secretly stack them somehow by using StartCoroutine() and keep them somewhere even though they aren’t doing anything anymore.
I know that if I put an infinite while loop in a coroutine, the coroutine will never stop until you tell it to do so (via StopCoroutine(), StopAllCoroutines() or a boolean set in the while loop).
But even then, would it really end? Take a look at this simple example:
IEnumerator TestCoroutine()
{
yield return new WaitForSeconds(0.75f);
DoSomethingHere();
yield return new WaitForSeconds(3f);
DoSomethingElseHere();
yield break; //Is this even needed?
}
Would this coroutine end itself once it reaches the last line, since it is not in an infinite while or for loop? Because if yes, calling it several times (like 4 or 5 times every here and there) wouldn’t affect performance overhead at all… right?
I’m just not sure about the usage of these coroutines, maybe it’s just me but I have the weird feeling that if I start several of these coroutines over a timespan of many hours of gameplay they never terminate themselves (even though they “should”) and get stacked somewhere where they shouldn’t, and therefore make the performance suffer a lot.
Thanks in advance for your help,
-Raphael