Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 A_Lego · Oct 18, 2018 at 06:29 AM · scripting problemfunction callfunction-type

Stop all instances of same coroutine?

So I call the same coroutine multiple times, which creates multiple instances of the coroutine. This works perfectly, the issue arrises when I want to stop ALL of the instances of that coroutine. I have other coroutines running simultaneously so the StopAllCoroutines wont be of use to me(Unless there's a way to specify it to a specific coroutine "type"). I also don't know how many instances of said coroutine there are as they are generated by the program itself. For Instance:

 void Start(){
     StartCoroutine("SomethingImportant");
 }
 void Update(){
     //If you press the W key, an instance of "TheCoroutine" will be initiated
     if(Input.GetButtonDown(KeyCode.W)){
         StartCoroutine("TheCoroutine");
     }
     //If you press the S key, all the previous instances of "TheCoroutine" will be destroyed
     if(Input.GetButtonDown(KeyCode.S)){
         //This is the part where I want to end all instances of "TheCoroutine"
         //without ending "SomethingImportant"
     }
 }
 IEnumerator TheCoroutine(){
     //Do something
     //This may be called multiple times which creates multiple instances
     //of this coroutine
 }
 IEnumerator SomethingImportant(){
     //Something important that I don't want to stop
 }

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

1 Reply

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

Answer by Casiell · Oct 18, 2018 at 06:34 AM

StartCoroutine returns an object of type Coroutine. You can keep them in a list and then close all of them with StopCoroutine.

I would have to test what happens when you use string arguments to start and stop a coroutine. Because with IEnumerator and Coroutine argument types you have a reference to a specific coroutine, but with string you do not, so I wonder what happens.

Edit: Ok, if you are using string arguments for starting, you can also use a string for stopping and it will stop all coroutines with this name. Here is a simple code that I created to test that:

 public class Coroutines : MonoBehaviour 
 {
     private readonly WaitForSeconds _wait = new WaitForSeconds(0.1f);
     private int _i;
 
     private void Start()
     {
         for (int i = 0; i < 10; i++)
         {
             _i = i;
             StartCoroutine("Coroutine");
         }
     }
 
     public void StopCoroutines()
     {
         StopCoroutine("Coroutine");
     }
 
     private IEnumerator Coroutine()
     {
         int i = _i;
         while (true)
         {
             Debug.Log(i.ToString());
             yield return _wait;
         }
     }
 }
 
 [CustomEditor(typeof(Coroutines))]
 public class CoroutinesInspector : Editor
 {
     public override void OnInspectorGUI()
     {
         base.OnInspectorGUI();
 
         if (GUILayout.Button("Stop coroutines"))
         {
             ((Coroutines)target).StopCoroutines();
         }
     }
 }


Comment
Add comment · Show 1 · 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 A_Lego · Oct 20, 2018 at 05:42 AM 0
Share

Thanks for your reply! I ended up just making a list of the IEnumerators then just using StopCoroutine in a loop to stop them. Though it is good to know that the string version of StopCoroutine stops all of the coroutines with that name.

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

165 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Do debug.log messages show up in unity console in exactly the sequence those statements are called? 0 Answers

How can I call functions in multiple scripts, if they have the same name? 1 Answer

Unable to change a variable from another script. 2 Answers

How can I slowly rotate a vector2 to track a target and give velocity based on the angle. 1 Answer

Why when using EditorWindow script type and MenuItem it's not add it to Hierarchy right click mouse context menu ? 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