- Home /
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.
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.
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.
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.
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.
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?
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.
I had the same issue (in Unity 5.3.5) and it worked for me. thanks!
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);
Indeed... I just came on here to update my answer ;)
Your answer
Follow this Question
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