Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Phoenixst · May 26, 2012 at 11:11 AM · coroutine

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Karsnen_2 · May 11, 2013 at 06:44 AM 0
Share

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?

avatar image Bunny83 · May 11, 2013 at 09:15 AM 4
Share

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.

avatar image
0

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 !

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Screenshot of my console

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.


coro.png (83.9 kB)
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Berenger · May 26, 2012 at 08:22 PM 0
Share

Is it a script attached to /Box that call StopAllCoroutines ? The same GameObject that has ClassA attached.

avatar image Phoenixst · May 26, 2012 at 08:33 PM 0
Share

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! :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Turn bool on/off x time every few seconds in coroutine 2 Answers

Active Battle System implementation question. 1 Answer

Start Coroutine after other has finished 4 Answers

Why does this coroutine freeze my game? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges