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 Mene · Nov 27, 2013 at 11:53 PM · coroutines

Stop unnamed Coroutine instance

When I start a Coroutine like this

 StartCoroutine(MyCoroutine());

Is there any way to stop it again? And if not, what is the point of the Coroutine-Class?

edit

MyCoroutine has parameters, so it actually looks like this:

 StartCoroutine(MyCoroutine(a, b, c));

And I might have multiple instances of the Coroutine running simultaniously.

The only solution I could come up with is to dynamically add empty MonoBehaviors to the GameObject and run each Coroutine in that behavior. But that seems very dirty to me (I did not yet actually try this).

Also I wonder what the point of the Coroutine-Class is.

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

Answer by Key_Less · Nov 28, 2013 at 01:19 AM

You can check out the Unity script reference page for StopAllCoroutines() here.

With this approach, just be aware that calling StopAllCoroutines() will stop all coroutines you have running on that behavior, which may lead to undesired results if you have more than one coroutine running. If you want to stop that individual coroutine, then you will need to call and stop it like this:

 StartCoroutine("MyCoroutine");
 StopCoroutine("MyCoroutine");

You can read more about coroutines here.

Comment
Add comment · Show 5 · 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 Mene · Nov 28, 2013 at 05:13 PM 0
Share

Thank you. I already know that, but my coroutines are parameterized and thus I cannot use the string variant of StartCoroutine and thus I asked if there is a way of stopping Coroutines by their handle, and if not why they have a handle at all, because I cannot find any $$anonymous$$ethod taking it as parameter.

avatar image Key_Less · Nov 28, 2013 at 09:11 PM 0
Share

You are incorrect. If you read the link to Unity's Script Reference page regarding coroutines, you'd see that calling a coroutine using the string method allows you to pass in a parameter. Although it only allows for one parameter, it is of type object. $$anonymous$$eaning you can pass in any predefined or user-defined types. You could, for example, pass in a list of game objects or perhaps a struct that contains all the data you want to use in your coroutine.

 // ...
 var myStruct = new $$anonymous$$yStruct();
 // Initialize data in struct.
 // ...
 
 // Pass your struct into the coroutine.
 StartCoroutine("$$anonymous$$yCoroutine", myStruct);

Now you can access the data from your struct.

 public IEnumerator $$anonymous$$yCoroutine($$anonymous$$yStruct data)
 {
    // Access your struct here
    // Ex: data.myStructData;
    yield return null;
 }

This is just an example of what you could do. I don't know what you are using as parameters.

avatar image Mene · Nov 28, 2013 at 09:18 PM 0
Share

Sorry, I was unclear. Yes, I can use it to start the coroutines, but if i have started the same coroutine twice, but with different parameters, how can I tell which ones to stop?

avatar image Key_Less · Nov 29, 2013 at 01:56 AM 0
Share

Calling stop on a coroutine will effect only the coroutines that are running on that particular behaviour. I need to ask, are you calling the same coroutine once from multiple objects, or are you calling the coroutine multiple times from only one object?

I don't know of any way to keep track of and stop a specific coroutine out of multiple instances. Perhaps we can find another solution to your issue if you shed some light on why you are calling the same coroutine multiple times?

avatar image Mene · Nov 29, 2013 at 11:03 AM 0
Share

I use them for animations. Yes several run on the same Behavior, see the edit of my initial question.

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

17 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

Related Questions

Coroutines vs Update/FixedUpdate/LateUpdate 2 Answers

How to Invoke() .. one frame 2 Answers

Waiting for Input via Coroutine 2 Answers

If I StopCoroutine("myCoroutine")', will the variable values in myCoroutine be reset? 1 Answer

Flash image while ammo is low - coroutines? 3 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