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.

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.