- Home /
Stop All Coroutines
**`MonoBehaviour.StopAllCoroutines`** > Stops all coroutines running on this > behaviour.
Does this note found on StopCoroutines
help page also applies to StopAllCoroutines
???
Because while I'm testing it seems so. The objects' co-routines won't stop after calling StopAllCoroutines
a Fixed Frame before destroying the object. Can anyone confirm or am I missing something here?
Thanks
Answer by Bunny83 · May 26, 2012 at 03:29 PM
StopAllCoroutines should stop all coroutines. Any coroutine have to be stored in a structure inside the MonoBehaviour, no matter if you start it with a string or passing the IEnumerator manually.
StopCoroutine only works with coroutines which have been started with a string since if you pass in a IEnumerator, StartCoroutine doesn't know the functions name so it is just stored as a coroutine. It's actually a shame that there's not a version of StopCoroutine that takes a Coroutine reference. StartCoroutine returns a reference to the coroutine structure used internally.
Anyway StopAllCoroutines should stop all running coroutines. I'm not sure if they are removed immediately or at the end of the current frame. I guess it's delayed like Destroy to avoid problems when you call StopAllCoroutines in a coroutine.
Btw, when you destroy the MonoBehaviour, all coroutines are also terminated since the behaviour will get erased from memory so the internal coroutine list is also gone.
Lets say I have two classes and each class have no dependency on each other. Now both the classes have co routines running. Hence if I tend to use StopAllCoroutines() in script B will it stop the routines only script B or will it stop the routine running in A also?
It will stop only the coroutines on script B. StartCoroutine, StopCoroutine and StopAllCoroutines are member-functions. So they are bound to the instance you call them on.
If you have a reference to the script A instance you can use this to stop coroutines on this instance from script B by using:
ScriptAref.StopAllCoroutines();
$$anonymous$$eep in $$anonymous$$d that this reference can also be of type $$anonymous$$onoBehaviour, so you don't couple the two classes too tight.
Also don't forget if you destroy the $$anonymous$$onoBehaviour the coroutine is running on it will be ter$$anonymous$$ated.
Answer by Berenger · May 26, 2012 at 03:02 PM
[False !]
Apparently, Unity is storing the coroutines in some data structure, a dictionary I'd say, where they associate them with the string you pass. That's why you can Stop them by name, and StopAll is going to go through that data structure to stop each one of them. However, if you start the coroutine directly with the function signature, it cannot be stored. So yes I confirm.
But I'm only guessing from my experiences.
[/False !]
I tried it out with that code, and indeed, StopAllCoroutines stop them all !
Answer by Phoenixst · May 26, 2012 at 08:01 PM
Thanks Bérenger Mantoue and Bunny83.
I'm still getting the issue over here. I think because of the way I start my co-routines. I use the C# event system to start co-routines.
Class A
//Define type
public delegate IEnumerator SomeHandler(SomeClass args);
//Define Event
public event SomeHandler SomeEvent;
//Invoke
if(SomeEvent != null)
StartCoroutine(SomeEvent(args));
Class B
//Register
transform.Find("/Box").GetComponent<ClassA>().SomeEvent
+= new SomeEventHandler(DoSomething);
IEnumerator DoSomething(SomeClass args)
{
} // This one that keeps running after StopAllCoroutines
I'm starting to think my problem is related to the fact co-routines will belong to the MonoBehavior that invoked these co routines. rather than the class that listens to them.
Running StopAllCoroutines on the class that is listening seems to me that it does nothing. not even on object destruction.
Is it a script attached to /Box that call StopAllCoroutines ? The same GameObject that has ClassA attached.
Thanks a lot for information it helped discover what is going on!
$$anonymous$$y issue was that I used to trigger co-routines using C# events through Class A
. Then listen to these events on Class B
.
The IEnumerator
methods in ClassB
triggered by the events, were originally started in ClassA
when the event was invoked. Therefore, StopAllCoroutines
and OnDestroy
in ClassB
weren't seeing these co-routines which belong to ClassA
. They kept running and causing Null Reference errors.
I changed the listening methods in ClassB
to standard methods that start the co-routines under ClassB
's name and everything seems fine! :)