How far does StopAllCoroutines() reach?

It was mentioned that it will stop all coroutines in the Player.cs script, but I assume that means the Player class right? If I were to add another class onto that script, would coroutines in that new class be stopped as well?

Hi J,

From the documentation;

Stops all coroutines running on this behaviour.

I would read that as all coroutines running on that instance of the class inheriting from MonoBehaviour.

So, in a scenario where the same script was on multiple GameObjects, if one of those GameObjects has the StopAllCoroutines method called, they wouldn’t all stop on the other GameObjects, which have the same script as a component also.

With regards to multiple scripts on a single GameObject, again, it will only stop the coroutines in the associated class, not other classes on the same GameObject.

I cobbled a little example together for you, it just outputs messages to the console at different times and waits for keyboard input. Having a look at the code in Shape.cs and AnOther.cs.

If you run the game you’ll find that pressing C stop all the coroutines on the cube which are running from the Shape component. Pressing S stops all the coroutines on the sphere running from the Shape component. They are treated separately, despite using the same Shape.cs class.

If you press A it will stop all of the coroutines within the AnOther.cs class attached to the cube. Again, this is treated separately from the Shape.cs class, so the coroutines already running within the Shape component will keep running (assuming you haven’t already stopped them).

StopAllCoroutines.zip (3.8 KB)

(.zip file contains a .unitypackage which you can import)

Hope this helps :slight_smile:


See also;

1 Like

Privacy & Terms