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
3
Question by ratmstein · Sep 15, 2013 at 09:11 PM · wont stop

How do I stop coroutine?

I have a coroutine that creates spheres. I want the coroutine to stop after creating 200 spheres. I have a counter in another class that counts the number of spheres created. I have tried using StopCoroutine(), but no luck. Any ideas??

Comment
Add comment · Show 2
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 Fattie · Oct 10, 2013 at 02:52 PM 0
Share

Just for anyone searching in the future, "yield break;" is a real tip, that catches out new coroutiners! it's how you "leave" a coroutine.

@ratmstein you should TIC$$anonymous$$ an answer in questions, thanks

avatar image ArkaneX · Oct 10, 2013 at 03:02 PM 0
Share

@Fattie - thank you for accepting!

2 Replies

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

Answer by ArkaneX · Sep 15, 2013 at 09:38 PM

Please note, that StopCoroutine method will only stop coroutines called with StartCoroutine overload, that accepts coroutine name as string. If you have coroutine:

 IEnumerator MyCoroutine()
 {
     // content
 }

and you call it using

 StartCoroutine(MyCoroutine());

then you won't be able to stop it using

 StopCoroutine("MyCoroutine");

For the StopCoroutine method to work, you have to start it using

 StartCoroutine("MyCoroutine")

Apart from above, you can stop a coroutine in a different way. For example you can use

 IEnumerator MyCoroutine()
 {
     for(var i = 0; i < 200; i++)
     {
         // instantiate your sphere
         yield return null;
     }
 }

After exiting for loop, coroutine will stop. You can also use yield break to stop execution of coroutin, if specific condition is met:

 IEnumerator MyCoroutine()
 {
     while(true)
     {
         // instantiate your sphere
 
         if(/*some condition check*/)
         {
             yield break;
         }
         else
         {
             yield return null;
         }
     }
 }


Comment
Add comment · Show 9 · 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 Jamora · Sep 15, 2013 at 09:43 PM 3
Share

You forgot [StopAllCoroutines][1](). It ends all currently active coroutines, regardless of how they were started.

I would also like to add that Coroutines are run on the GameObject. So disabling the gameobject will cause any coroutine running on it to stop. You can also turn on/off coroutines on other GameObjects by gaining a reference to the $$anonymous$$onoBehaviour that started it on the GameObject and use Start/StopCoroutine or StopAllCoroutines.

[1]: http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.StopAllCoroutines.html

avatar image ArkaneX · Sep 15, 2013 at 09:57 PM 2
Share

@Jamora - I tried to describe methods of stopping one particular coroutine only. And you're right that disabling game object is one of them.

avatar image ArkaneX · Sep 16, 2013 at 07:18 AM 2
Share

When you get any error message, please always include it when you post here. It helps us to identify the problem.

As to your problem, please show your coroutine method signature and the line where you try to start it.

avatar image ArkaneX · Sep 16, 2013 at 02:49 PM 2
Share

You can then call it using

 StartCoroutine("changeDirectionAfterDelay", seconds);

Please note that you have to change last line of your coroutine to above line as well.

And you can stop it using

 StopCoroutine("changeDirectionAfterDelay");
avatar image ArkaneX · Sep 16, 2013 at 02:54 PM 2
Share

Ins$$anonymous$$d of calling the coroutine again at the end of execution, you can also change it to:

 public IEnumerator changeDirectionAfterDelay(float seconds)
 {
     while(true)
     {
         yield return new WaitForSeconds(seconds);
         direction = UnityEngine.Random.onUnitSphere;
     }
 }
Show more comments
avatar image
9

Answer by ObviousDWest · Apr 07, 2017 at 10:44 AM

At some point, StopCoroutine(Coroutine c); was added to the MonoBehavior interface. So it appears you can also do:

  Coroutine theCoroutine = StartCoroutine(MyCoroutine());

and later:

 StopCoroutine(theCoroutine);
Comment
Add comment · Show 3 · 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 Sun-Dog · Mar 15, 2018 at 06:20 PM 0
Share

This worked really well and is exactly what I was looking for.

avatar image karsnen · Feb 18, 2019 at 09:44 PM 0
Share

This is the most appropriate answer.

avatar image bekiryayla · Apr 08, 2020 at 10:58 AM 0
Share

this is not work

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

22 People are following this question.

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

Related Questions

For Loop Won't Stop Running... 1 Answer

I can't create a world border? 1 Answer

Particle system stop? 3 Answers

How to make Timer stop upon Death? 1 Answer

Stop player movement if it's Raycast hits an object 0 Answers


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