Interrupting an Enemy Attack [SOLVED]

Hello All!

I have an enemy which works fine. What I want to do is interrupting it’s attack if it gets damaged during the attack animation. I use below script which interrupts the attack animation and plays idle animation instead. The problem is that the damage from enemy attack still registers.

I used coroutines to start and stop attacking. And to call StopCoroutine(EnemyAttack); I used a static boolean InflictDamage.AttackRecieved == true from another script. Since animation stops which has no bound to the TakingDamage() coroutine, I assume my boolean InflictDamage.AttackRecieved working fine. I believe the stop coroutine function either doesn’t work or working very slowly that it can’t catch damage register part. Any good solutions for this kind of a issue?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpiderAI : MonoBehaviour
{
    public GameObject ThePlayer;
    public float TargetDistance;
    public float AllowedRange = 40;
    public GameObject TheSpider;
    public float EnemySpeed;
    public int AttackTrigger;
    public RaycastHit Shot;
    public int DealingDamage;
    public Animation AttackAnim;
    IEnumerator EnemyAttack;

   

    void Update()
    {
        EnemyAttack = TakingDamage();
        transform.LookAt(ThePlayer.transform);
        if (InflictDamage.AttackRecieved == true)
        {
            TheSpider.GetComponent<Animation>().Play("idle");
            StopCoroutine(EnemyAttack);
            Invoke("notdeal", 0.5f);
        }

        else
        {

            if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out Shot))
            {
                TargetDistance = Shot.distance;
                if (TargetDistance <= AllowedRange)
                {

                    EnemySpeed = 0.07f;
                    if (AttackTrigger == 0)
                    {
                        TheSpider.GetComponent<Animation>().Play("walk");
                        transform.position = Vector3.MoveTowards(transform.position, ThePlayer.transform.position, EnemySpeed);

                    }

                }

                else
                {
                    EnemySpeed = 0f;
                    TheSpider.GetComponent<Animation>().Play("idle"); // animation name in quotation mark.


                }

            }


            if (AttackTrigger == 1)
            {
                if (DealingDamage == 0)
                {
                    EnemySpeed = 0f;
                    TheSpider.GetComponent<Animation>().Play("attack");
                    StartCoroutine(EnemyAttack);
                }

            }

        }



    }

    void OnTriggerEnter()
    {
        AttackTrigger = 1;
        
    }

    void OnTriggerExit()
    {
        AttackTrigger = 0;  
    }

    void notdeal() 
    {
        DealingDamage = 0;
    }
    IEnumerator TakingDamage() 
    {
        DealingDamage = 2;
        
        yield return new WaitForSeconds(AttackAnim["attack"].length * (1 / AttackAnim["attack"].speed));
       
        
            if (SpiderEnemy.GlobalSpider != 0)
            {
                HealthMonitor.HealthValue -= 1;
            }
            yield return new WaitForSeconds(0.4f);
            DealingDamage = 0;
  
    }
}

Maybe make another bool “enemyDamaged”. When enemy takes damage and goes to his idle animation set enemyDamaged=true. when he starts another attack, reset it to enemyDamaged=false;
then as a condition to the player taking damage if(enemyDamaged==false)…perhaps this would be in the coroutine? somewhere, wherever the code is that instructs damage to be assessed