Stop All Coroutines

MonoBehaviour.StopAllCoroutines
Stops all coroutines running on this
behaviour.

Does this note found on StopCoroutines help page also applies to StopAllCoroutines ???

Please note that only StartCoroutine
using a string method name can be
stopped using StopCoroutine.

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

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.

[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 !

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.