- Home /
Coroutines Bug? Unity 4.5
So the situation is i have two monobehaviours(object1 and object2) object1 is running coroutine that waits for coroutine running on object2 and the problem is that when i call object2.StopAllCoroutine() object2 coroutine stops, but coroutine on object 1 does not resume
Example:
public class RootObject: MonoBehaviour
{
private CoroutineHost coroutineHost;
public void Start()
{
var go = new GameObject();
coroutineHost = go.AddComponent<CoroutineHost>();
this.StartCoroutine(GamePlayRoutine());
this.StartCoroutine(StopAllCoroutinesRoutine());
}
public IContained<IGameplay> gameplay
{
get;
set;
}
private IEnumerator GamePlayRoutine()
{
yield return coroutineHost.StartCoroutine(gameplay.contained.Run());
Debug.Log("!!!!");
gameplay.Dispose();
}
private IEnumerator StopAllCoroutinesRoutine()
{
yield return new WaitForSeconds(1.0f);
Debug.Log("Killing Coroutines");
coroutineHost.StopAllCoroutines();
}
}
In this example only Killing Coroutines is being printed, but never "!!!!"
Answer by Tarlius · Jun 10, 2014 at 08:48 AM
I don't think its a bug, its probably a design decision.
If you force quit a program, do you expect it to (or it to be in a fit state to) return a value to its caller?
Its an odd case though!
I think to solve your problem you will want to make something like a EndGame method which will set a flag to let the coroutine end itself correctly, or just not nest the coroutine in the first place.
I don't agree with analogy about quitting program, as i am not trying to get return value of ter$$anonymous$$ated program, but just tracking if the program has ter$$anonymous$$ated.
But i can now agree that it is probably not a bug as it is the case in all the other unity versions
Since coroutines are really just IEnumerators, I think the analogy is more valid than you might think at first. I do wonder about the implementation though and do agree that it feels like you are "tracking" it.
I'm well aware that coroutines are just on which Unity call $$anonymous$$oveNext every frame(except for specific yield instructions)
But i'm still sure that the sematics of yield return on Coroutine is to wait for the other coroutine to be finished, and after i stop the coroutine by using StopAllCoroutines coroutine really is finished.
This situation leads to a lot of problems(leaving coroutine hanging indefinetly is in the best case a memory leak) that are rendering this function useless in my opinion