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
12
Question by douglasg14b · Mar 28, 2015 at 05:31 AM · coroutineloopattackwhile

StopCoroutine() is not stopping my coroutines

I'm getting some attacking to work with my units and I noticed that the coroutines seem to start adding up even after the StopCoroutine(IEnumerator) as need called.

I dug around on ways to perform an attack loop, most of the time I found "Use InvokeRepeating" and "Don't use InvokeRepeating". Since invoke uses reflection it's performance heavy anyways, so I'd rather not go down that road.

How do I get my coroutine to stop? Is it because it's in a while loop inside the coroutine? If so, how do I fashion an attack loop without using invoke?

 //Recieves call when a new enemy is added to the list
 public void NewEnemyDetected()
 {
     if(currentlyAttacking == null)
     {
         SelectEnemyToAttack();
     }
 }

 //Recieves call when an enemy leaves the collider area
 public void EnemyGone()
 {
     SelectEnemyToAttack();
 }

 //Will select an enemy from the array to attack
 private void SelectEnemyToAttack()
 {
     if(enemyObjects.Count > 0)
     {
         StopCoroutine(AttackEnemy());
         currentlyAttacking = enemyObjects.ElementAt(0);
         isAttacking = true;
         StartCoroutine(AttackEnemy());
     }
     else
     {
         currentlyAttacking = null;
         isAttacking = false;
     }
 }

 //Partially hacked together attacking loop
 private IEnumerator AttackEnemy()
 {
     while(isAttacking)
     {
         Vector3 angle = currentlyAttacking.DetectedGameObject.transform.position - transformV.position;
         angle = angle.normalized;
         GameObject projectile = (GameObject)Instantiate(AttackType, transformV.position, transformV.rotation);
         BlasterAttack projectileAttack = projectile.GetComponent<BlasterAttack>();
         projectileAttack.OnCreation(10, player);
         projectile.GetComponent<Rigidbody>().AddForce(angle * 100);
         yield return new WaitForSeconds(1);
     }
     yield break;
 }

Thanks!

Edit:

StopAllCoroutines() does stop the coroutine.

Edit2: I just went back and changed it all back to by name again per Huacanacha's suggestio and it's working. Maybe I missed something the first time I tried that.

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 maccabbe · Mar 28, 2015 at 05:54 AM 0
Share

You could try using StopAllCoroutines();

Also it seems a little strange that you are selecting a new target every time an enemy leaves the collider area, even if the enemy that left was not the old target.

avatar image douglasg14b · Mar 28, 2015 at 06:04 AM 0
Share

StopAllCoroutines() works. However it does not seem like an ideal solution, but I can use it for now.

Thanks for catching that, I slapped this together really fast to try and get some basic form of attacking working. Patched that up, didn't make much sense.

1 Reply

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

Answer by Huacanacha · Mar 28, 2015 at 05:38 AM

New answer:

In more recent versions of Unity (at least 5.3 onwards) you can keep a reference to the IEnumerator or returned Coroutine object and start and stop that directly, rather than use the method name. These are preferred over using the method name as they are type safe and more performant. See the StopCoroutine docs for details.

 Coroutine lastRoutine = null;
  
 lastRoutine = StartCoroutine(YourCoroutine());
  
  // [ ... ]
  StopCoroutine(lastRoutine);


Old answer - OUT OF DATE:

You need to start the Coroutine by the method name rather than by calling the function. Per the Unity docs for StopCoroutine: "Please note that only StartCoroutine using a string method name can be stopped using StopCoroutine."

So start and stop your Coroutine like this:

 StartCoroutine("AttackEnemy");
 ...
 StopCoroutine("AttackEnemy");

I believe in Unity 5 you may be able to store the returned Coroutine object and call StopCoroutine using that.

Comment
Add comment · Show 7 · 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 douglasg14b · Mar 28, 2015 at 05:58 AM 0
Share

Thank you for the reply.

I tried starting it by name ins$$anonymous$$d of by method and it still will not stop, I also tried stopping it by name ins$$anonymous$$d of by method with no favorable results.

avatar image Huacanacha · Mar 28, 2015 at 06:04 AM 0
Share

Are you also stopping the Coroutine using the method name?

If you are then try using StopAllCoroutines() ins$$anonymous$$d... what happens if you do that?

avatar image douglasg14b · Mar 28, 2015 at 06:11 AM 0
Share

StopAllCoroutines() does it.

I just went back and changed it all back to by name again and it's working. $$anonymous$$aybe I missed something the first time I tried that. Thank you.

avatar image Zagule · Aug 23, 2016 at 06:33 PM 0
Share

I had the same issue (in Unity 5.3.5) and it worked for me. thanks!

avatar image Bunny83 Zagule · Aug 23, 2016 at 06:52 PM 4
Share

The recent Unity versions support stopping coroutines by either use the Coroutine object that is returned by StartCoroutine, or by using the same IEnumerator instance that you used to start the Coroutine. The better solution is using the Coroutine object.

 Coroutine lastRoutine = null;
 
 lastRoutine = StartCoroutine(YourCoroutine());
 
 // [ ... ]
 StopCoroutine(lastRoutine);

avatar image Huacanacha Bunny83 · Aug 23, 2016 at 07:13 PM 2
Share

Indeed... I just came on here to update my answer ;)

Show more comments

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

24 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

Related Questions

Coroutine While loop help 2 Answers

Stop Current Coroutine, Then Restart It 1 Answer

How to wait for Coroutine to finish? 2 Answers

Waiting for a mouse click in a Coroutine 1 Answer

Problem with while statement. 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