Health Regen After X seconds

Trying to get health to regen after being hit. I’ve tried a few different ways but this is where I get stuck: each time I add functionality for a wait time between getting hit and regaining health. I got it to work at a stage, but then when the player takes, say, 1 damage and a second later 2 damage, it will run either two coroutines in parallel, or just regen health after it took the first amount of damage (1 in this case).

Please help! :stuck_out_tongue:

public class NewPlayerStats : MonoBehaviour
{

public int startingHealth = 5;
public int currentHealth;
public int healthReg = 1;

bool isRegenHealth;

void Awake()
{
    currentHealth = startingHealth;
}

void Update()
{
    if (currentHealth != startingHealth && !isRegenHealth)
    {
        StartCoroutine(RegainHealthOverTime());
    }
}

private IEnumerator RegainHealthOverTime()
{
    isRegenHealth = true;
    while (currentHealth < startingHealth)
    {
        Healthregen();
        yield return new WaitForSeconds(1);
    }
    isRegenHealth = false;
}

public void Healthregen()
{
    currentHealth += healthReg;

}

}

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

public class NewPlayerStats : MonoBehaviour
{
    [SerializeField]
    private int maxHealth = 5;

    [SerializeField]
    private float healthRegenerationRate = 1;

    [SerializeField]
    private float regenerationDelay = 1;

    private int currentHealth;

    private float lastDamagesTime = -1;

    private float regeneratedHealth;

    public bool IsDead
    {
        get { return currentHealth == 0; }
    }

    private void Awake()
    {
        currentHealth = maxHealth;
    }

    void Update()
    {
        if ( lastDamagesTime >= 0 && Time.time - lastDamagesTime >= regenerationDelay )
        {
            RegenerateHealth();
        }
    }

    public void RegenerateHealth()
    {
        regeneratedHealth += healthRegenerationRate * Time.deltaTime;
        int flooredRegeneratedHealth = Mathf.FloorToInt( regeneratedHealth );
        regeneratedHealth -= flooredRegeneratedHealth;
        Heal( flooredRegeneratedHealth );
    }

    public void Hurt( int damages )
    {
        if ( damages < 0 )
            throw new System.ArgumentException( "Damages must be greater or equal to 0", nameof( damages ) );

        if ( IsDead )
            return;

        currentHealth = Mathf.Clamp( currentHealth - damages, 0, maxHealth );
        regeneratedHealth = 0;
        lastDamagesTime = Time.time;

        if( IsDead )
        {
            // Die
        }
    }

    public void Heal( int amount )
    {
        if ( amount < 0 )
            throw new System.ArgumentException( "Healed amount must be greater or equal to 0", nameof( amount ) );

        currentHealth = Mathf.Clamp( currentHealth + amount, 0, maxHealth );
        if( currentHealth == maxHealth )
        {
            lastDamagesTime = -1;
            regeneratedHealth = 0;
            // Full health
        }
    }
}