Stop enemy attack coroutine when player is death.

Hi! i have a coroutine for the enemy attack, i want to stop this coroutine when the player is dead, but i have this error. I stopped other coroutines same this form, but this not work. Thanks :slight_smile:

add this line of code instead of.

private IEnumerator coroutine;

//where you strat your couritune

coroutine = Attack(time);
StartCoroutine(coroutine);

//call this to stop routine 

StopCoroutine(coroutine);

Try changing coroutine variable type to Coroutine instead of IEnumerator, like this:

          public Coroutine coroutine;
         void CheckDistance()
              {
                  if (Vector3.Distance(target.position,
                      transform.position) <= chaseRadius
                      && Vector3.Distance(target.position, transform.position) > attackRadius)
                  {
                      if (currentState == EnemyState.idle || currentState == EnemyState.walk
                          && currentState != EnemyState.stagger)
                      {
                          ChangeState(EnemyState.attack);
                          anim.SetTrigger("attack");
                          print("rana ataca");
                          if (!attacking)
                          {
                              coroutine = StartCoroutine(Attack(waitToAttack));
                          }           
                      }
                  }
                  
                  
                  
                  
                  
                  HEALTH SCRIPT
          
          if (health <= 0)
                  {
                      rb2d.constraints = RigidbodyConstraints2D.FreezePosition;
                      print("player muerto");
                      anim.SetBool("Death", true);
                      swordDamage.Instance.boxColliderSword.enabled = false;
                      playerColl.enabled = false;
                      gameObject.tag = "Untagged";
                      frog.Instance.StopCoroutine(frog.Instance.coroutine);
                  }