Hurting enemy when attacking status is equal to true and player is colliding with enemy object.

Hi, I am trying to make a game where you can stab a enemy with a knife, the problem is that I don’t know how to check if the enemy collides with the player while a attack boolean is set to true, then it will set the health of the enemy to -= 5 every time. Does anyone know how to do this?

Here’s the player srcipt:

static var IsAttacking = false;
var TimeD : float = 1;




function Update () {

 IsAttacking = true;
yield WaitForSeconds(TimeD);
 IsAttacking = false;
}

Here’s the enemy script:

var Health : float = 100;
var Self : GameObject;


function OnTriggerEnter () {
   
          if(PlayerScript.IsAttacking == true){
          
            Health -=5;
          
            if(Health == 0){
          
              Destroy (Self);
          
          
              }
          }
          
         
          //Nothing
          
          
          
        }

You can’t use a yield coroutine within function update()

I recommend moving

IsAttacking = true;

yield WaitForSeconds(TimeD);

IsAttacking = false;

into a new function and calling that when the player attacks.