How do I make an enemy only damage once per second?

void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.name == “KP”) {
StartCoroutine(DamagePlayer());
DamagePlayer();
StartCoroutine(DamagePlayer());
}
}

public IEnumerator DamagePlayer() { //waits but doesnt force code to stop, make it do that
yield return new WaitForSeconds(1);
GameObject.Find(“KP”).GetComponent().DamagePlayer(damage);
print(“Waited”);
}

My wait function does work in that it halts any damage being done to after one second, but all of the damage floods in after that one second. What I need is for the enemy to only deal damage once per every second and time the player hits the enemy, not constant damage while the player is touching the enemy.

Please use the code formatting option to enter code, otherwise it’s really hard to read it properly.

A solution off the top of my head would be to add a timer variable to the enemy script, like so:

float damTimer;
    
void Update(){
  if(damTimer > 0){
    damTimer -= Time.deltaTime;
  }
}
    
OnCollisionEnter2D(Collision2D col) { 
  if (col.gameObject.name == "KP") { 
    if(damTimer <= 0){
      DamagePlayer(); 
    }
  }
}  
 
public void DamagePlayer() {
  damTimer = 1;
  GameObject.Find("KP").GetComponent().DamagePlayer(damage);
}