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 Roixo · Feb 10, 2018 at 06:52 PM · stopcoroutine

StopCoroutine. how avoid to stop all my coroutines?

StopCoroutine stops all coroutines in the same behaviour, like it is said in this post and others similar:

link text

Just beware that if the same coroutine is called more than once using this method, then using StopCoroutine will result in all of them being stopped.

I am using the string call method, but it happens the same way using the IEnumerator call method.

Any idea how to stop just 1 coroutine and not all?

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

2 Replies

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

Answer by pako · Feb 10, 2018 at 09:03 PM

I've never used a StopCoroutine statement from inside the coroutine, and haven't seen such usage anywhere. I think this might be the problem.

You should use yield break instead of StopCoroutine to stop the coroutine from inside the coroutine.

BTW it would also be a good idea to have the Destroy (planetaTrozosGameObject) before the yield break

Comment
Add comment · Show 6 · 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 Roixo · Feb 10, 2018 at 09:44 PM 1
Share

Thanks so much.

It worked. I didn't know the yield break statement usage.

It also worked with the string method coroutine call.

Thanks again.

avatar image pako Roixo · Feb 10, 2018 at 09:45 PM 0
Share

You are welcome!

avatar image Roixo pako · Feb 10, 2018 at 09:53 PM 0
Share

Actually now i don't use the StopCoroutine statement, so i did't really test any StopCoroutine call method :)

Show more comments
avatar image
0

Answer by Hanoble · Feb 10, 2018 at 07:06 PM

The best way is to hold a reference to the IEnumerator and stop just that one, for example.

 //IEnumerator object to hold coroutine
 private IEnumerator myCoroutine;
 
 //Assign IEnumerator to the coroutine
 private void SomeMethod()
 {
         myCoroutine = WaitAndPrint(3.0f);
        StartCoroutine(myCoroutine);
 }
 
 //Kill the specific instance of the coroutine set earlier
 private void StopCoroutine()
 {
         StopCoroutine(myCoroutine);
 }

Further reading here.

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 Roixo · Feb 10, 2018 at 07:19 PM 0
Share

I tried that way, holding a reference to the IEnumerator, but it doesn't work neither.

avatar image pako Roixo · Feb 10, 2018 at 08:19 PM 0
Share

@Roixo this is impossible! Post your code!

avatar image Roixo pako · Feb 10, 2018 at 08:31 PM 0
Share

simplifying the code looks like this:

 private IEnumerator coroutine;
 
 void OnTriggerEnter (Collider other)
 {
     otherParentGameObj = other.transform.parent.gameObject;
     if (otherParentGameObj.tag == VariablesScript.TagPlaneta ) 
     {
 
         coroutine = LerpPlanetaHaciaPlayerCoroutine ();
 
         StartCoroutine (coroutine);
     }
 }
 
 
 private IEnumerator LerpPlanetaHaciaPlayerCoroutine ()
 {
     Vector3 planetaPosition = otherParentGameObj.transform.position;
     GameObject planetaTrozosGameObject = (GameObject) Instantiate (planetaTrozosPrefab, planetaPosition, Quaternion.identity);
 
     float fraccion = 0f;
     float distanciaConsideradaCercana = 1f;
 
     while (true)
     {
         try 
         {
             if (Vector3.Distance (planetaTrozosGameObject.transform.position, myParentGameObj.transform.position) >= distanciaConsideradaCercana)
             {
                 fraccion += 0.001f;
                 fraccion = ($$anonymous$$athf.Round (fraccion * 1000)) / 1000;
 
                 planetaTrozosGameObject.transform.position = Vector3.Lerp (planetaTrozosGameObject.transform.position, myParentGameObj.transform.position, fraccion);
             }
             else{
 
                 StopCoroutine (coroutine);
                 Destroy (planetaTrozosGameObject);
             }
 
         } catch (Exception e) {
 
             StopCoroutine (coroutine);
             Destroy (planetaTrozosGameObject);
         }
 
         yield return new WaitForFixedUpdate ();
     }
 
 }

In this post the author says that it didn't work for him neither:

link text

If you call multiple coroutines with same name than single StopCoroutine with that name will destroy them all (also started with IEnumerator).

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

76 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

Related Questions

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

Coroutine won't stop running. Tried String. 1 Answer

how to restart coroutine 2 Answers

Stopcoroutine by tapping screen 1 Answer

Can't Stop couroutines 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