Stopping constant update

What the code does is that when my player gets hit by the enemy it takes away a certain amount of health. If the player has not been hit for 10 seconds then the player get 5 point of life added to its health. If Another 10 seconds pass then the player again get another 5 points of life added. This is where im having trouble

When i run the code its waits for 10 seconds BUT then it fills up the whole bar the RestoreHealth method keeps getting called instead of checking if the player has been hit each frame. I think it has to do with me putting it into void Update since its updating the health every frame… Im not sure what you guys think ??

public class MyHealth : MonoBehaviour {


 public Slider healthBar;
 public float HitResetTimer; //Resetsthetimerto0whenihavebeenhitwithinthe10seconds;
 bool beenHit; //Returntrueorfalseifihavebeen hit
 float hitTimer; //keepstrackoftimefrom0to10ifihavenotbeen hit

 void Awake()
 {
 beenHit=false; //playerhasnotbeenhitso= false
 hitTimer=0.0f; //Setto0becauseihavenotbeenhityetsonotimeneedstoberegistered
 }

 void OnTriggerEnter(Collider other)
 {
 healthBar.value-=100;//Ifenemyhitscharacterloses10 points
 beenHit= true; //Issettotruebecauseihavebeen hit
 hitTimer= HitResetTimer;//IfcubehitssomethinghitTimerisnolongertimethathasoccuredbutresetto 0
 
 }

 void RestoreHealth()
 {
 if(!beenHit&& healthBar.value<100)
 healthBar.value+=2;
 }

 //Usethisfor initialization
 void Start () {


 
 }




 //Updateiscalledonceper frame
 void Update () {

 if(beenHit) //havenotbeen hit
 {


 hitTimer -=Time.deltaTime;

 if(hitTimer<0)
 {
 
 beenHit=false;
 }
 
 }
 else{
 RestoreHealth();


 }

 }
}

at the moment you have “if not ‘beenhit’ this frame, restore health” so yeah, it’s going to add health every frame.

if you want something to happen only once from an update call you need to add in some control to it.

bool canRegen = false;

void Update ()
{
    if(beenHit)
    {
        hitTimer -=Time.deltaTime;
   
        if(hitTimer<0)
        {
            beenHit=false;
            canRegen = true;
        }
    }
    else
    {
        if(canRegen)
        {
            RestoreHealth();
            canRegen = false;
        }
    }
}

or simplify it to

void Update ()
{
    if(beenHit)
    {
        hitTimer -=Time.deltaTime;
   
        if(hitTimer<0)
        {
           beenHit = false;
            RestoreHealth();
        }
    }
}

Oh okay I see so now the HealthBar only get called once. I have another question then (Sorry if im asking too many ) so i stepped through the code so

  1. I hit the enemy and the update begins to run
  2. X amount of time has passed since i last hit it
  3. Set beenHit Back to false since i am no longer being hit
    4.Since that time has passed i get X amount of health back

This is where i get confused. Since beenHit is set back to false shouldn’t hitTimer been set back to X amount of time and the if statement be ran again since its being constantly “Updated”. Right now after X amount of time passes X amount of health is restored but the Update Function is never ran again it just stops.

That is mostly my code and in the original post I didn’t think about reseting the timer once you healed so it didn’t auto heal every update its an easy fix. I changed some of the names of the variables to make them more understandable what they do:

using UnityEngine;
using UnityEngine.UI;
using System.Text.RegularExpressions;
using System.Collections.Generic;

public class ExplosionHandler : MonoBehaviour
{
    public class MyHealth : MonoBehaviour
    {
        public Slider healthBar;
        public float HealResetTimer; //Resetsthetimerto0whenihavebeenhitwithinthe10seconds;
        bool canHeal; //Returntrueorfalseifihavebeen hit
        float healTimer; //keepstrackoftimefrom0to10ifihavenotbeen hit

        void Awake()
        {
            ResetHealTimer();
        }

        void OnTriggerEnter(Collider other)
        {
            healthBar.value -= 10;//Ifenemyhitscharacterloses10 points
            ResetHealTimer();
        }

        void ResetHealTimer()
        {
            canHeal = false;
            healTimer = HealResetTimer;
        }

        void RestoreHealth()
        {
            if ( healthBar.value < 100)
                healthBar.value += 2;
            ResetHealTimer();
        }

        //Updateiscalledonceper frame
        void Update()
        {
            if (canHeal)
            {
                RestoreHealth();
            }
            else
            {
                healTimer -= Time.deltaTime;

                if (healTimer < 0)
                {
                    canHeal = true;
                }
            }
        }
    }
}