Regen a player's health after a set amount of time if the player has not been in combat

Hi, so I am trying to develop a script where the player regens their armour if they have not been in combat for a set period of time. Once the game begins, the enemy is randomly spawned so I can’t assign the enemy to a variable inside the inspector.

If anyone has any ideas, it’ll be greatly appreciated.

Thanks!

This should get you started. The question is a bit vague but perhaps this can give you a push in the right direction. This is pseudocode and only meant to indicate a possible direction you could take, the question is not specific enough to know exactly what you are looking for.

private bool inCombat;
private int maxHealth, currentHealth;
private float outOfCombatTimer, outOfCombatDelay;

[SerializeField] private int regenAmount;

void Update()
{
    if (!inCombat)
    {
        outOfCombatTimer += Time.deltaTime;

        if (outOfCombatTimer > outOfCombatDelay)
        {
            if (currentHealth < maxHealth)
            {
                Regen();
            }
        }
    }
}

void Regen()
{
    currentHealth += regenAmount;
}

You could have the enemy access a script attached to the player and reset the timer. Something like this.

PlayerManager.cs

PlayerManager : MonoBehaviour
{
    [SerializeField] private int nonCombatTimer;
    [SerializeField] private bool incrementingNonCombatTimer;

    [SerializeField] private float armourHealth;
    [SerializeField] private bool regeneratingArmour;

    private void Update() 
    {
        StartCoroutine(IncreaseNonCombatTimer());

        if (nonCombatTimer > 60) 
        {
            RegenerateArmour();
        }

    }

    private void TakeDamage(int damageAmount)
    {
         nonCombatTimer = 0;
    }

    private void RegenerateArmour()
    {
        //
    }

    private IEnumerator IncreaseNonCombatTimer()
    {
        if (!incrementingNonCombatTimer)
        {
            incrementingNonCombatTimer = true;
            yield return new WaitForSeconds(1);
            nonCombatTimer++;
            incrementingNonCombatTimer = false;
        }
    }

}

Enemy.cs

Enemy: MonoBehaviour
{
    private GameObject player;
    private PlayerManager playerManager;

    private void Start()
    {
        player = GameObject.FindWithTag("Player");
        playerManager = player.GetComponent<PlayerManager>();
    }

    void AttackPlayer()
    {
        playerManager.TakeDamage(10);
    }
}

Thanks everyone. I used the ideas from both solutions to develop my own, it works now. Yay!