Add 1 health point every second. (Regenerate Health)

I am making a survival game and I want to make my player regenerate one health point every second until it reaches 100 and then stops. Any ideas on how to script this would be greatly appreciated. Thanks.

Here’s an alternate coroutine for the job. (C#)

void Start() {
   StartCoroutine(RegenerateHealth());
}
 
IEnumerator RegenerateHealth() {
   while (true){
       health = Mathf.Clamp(health + 1, -100,100);
       yield return new WaitForSeconds(1);
   }
}

This coroutine is basically set and forget. Also worth noting there is nothing in the Update loop that is checked every frame, so this method will perform better.

This might help, got some free time…
bool onRegenHealth = false;

void Update()

if(health < 100 && onRegenHealth == false )
{
   onRegenHealth = true;
   StartCoroutine("RegenHealth");
}

IEnumerator RegenHealth()
{
   health += 1;
   yield return new WaitForSeconds(1);
   onRegenHealth = false;
}

private float t = 0.0f;
private float threshold = 1.0f;
int health = 20;

void Update() {
     t += Time.deltaTime;
     if(t >= threshold) {
          t = 0.0f;
          health ++;
     }

}

Without coroutine:

void Start () {
       InvokeRepeating("AddHealth", 1, 1);
}

void AddHealth()
{
    if(health < 100)
       health++;
    else
        CancelInvoke("AddHealth");
}