How to regenerate player's health after timer countdown is done.
HI, I have a script that regenerates player's health after 5 seconds:
   float counter;
 
     void  Update (){
           counter += Time.deltaTime;
            if(counter > 30minutes){
               curHealth++;
                 counter = 0.0f; }
               if(curHealth > 5)
                  curHealth = 5;
                  }
 
               Which is attach to my player's health script. I want to start regenerating my player health back when timer is at 0. But I don't know how. This is my player health script:
 //Stats
 public int curHealth;
 public int maxHealth = 3;
 public Collider2D col;
 public PlayerHealth playerhealthRef;
 public TimeManager countDownTimer;
 float counter;
 public Animator anima; // drag the panel in here again
 private UI_ManagerScripts UIM;
 DateTime currentDate;
 DateTime oldDate;
 
 private GenerateEnemy generateEnemy = null;
 
 
 void Start ()
 {
     curHealth = maxHealth;
     currentDate = System.DateTime.Now;
     if (countDownTimer != null)
     {
         // countDownTimer.gameObject.SetActive(false);
         countDownTimer.Enable(false);
     }
     if (GameObject.Find("Spawner"))
     {
         generateEnemy = GameObject.Find("Spawner").GetComponent<GenerateEnemy>();
     }
 }
 
 
 void Update ()
 {
     counter += Time.deltaTime;
     if (curHealth > maxHealth) {
         curHealth = maxHealth;
     }
     if ((curHealth <= 0) && (!countDownTimer.gameObject.activeSelf)) {
         
         Die ();
     }
     if(counter > 5)
     {
         curHealth++;
         counter = 0.0f; 
     }
     if(curHealth > 3)
         curHealth = 3;
 }
 
 void Awake()
 {
     UIM = GameObject.Find ("UIManager").GetComponent<UI_ManagerScripts> ();
 }
 
 void Die() {
     UIM.EnableBoolAnimator(anima);
     PlayerPrefs.SetInt("RemainingLives", curHealth);
     PlayerPrefs.Save();
     
     if (generateEnemy != null)
     {
         generateEnemy.DestroyAllBlobs();
     }
     if (countDownTimer != null)
     {
         countDownTimer.Enable(true);
     }
 }    
 public void Damage(int dmg)
 {
     curHealth -= dmg;
 }
  }
 
               And this is my Timer countdown script:
 public Text timer;
 int minutes = 1;
 int seconds = 0;
 float miliseconds = 0;
 
 [Range(1, 59)]
 public int defaultStartMinutes = 1;
 public bool allowTimerRestart = false;
 public bool useElapsedTime = true;
 
 private int savedSeconds = -1;
 private bool resetTimer = false;
 
 private DateTime centuryBegin = new DateTime(2001, 1, 1);
 
 private float tickPerSecond = 10000000.0f;
 
 public void Enable (bool enable)
 {
     gameObject.SetActive(enable);
     ResetTime(); // force the timer to restart
 }
 
 void Awake ()
 {
     minutes = defaultStartMinutes;
     if (PlayerPrefs.HasKey("TimeOnExit"))
     {
         miliseconds = PlayerPrefs.GetFloat("TimeOnExit");
         savedSeconds = (int)miliseconds;
         
         if (useElapsedTime && PlayerPrefs.HasKey("CurrentTime"))
         {
             int elapsedTicks = (int)(DateTime.Now.Ticks / tickPerSecond);
             int ct = PlayerPrefs.GetInt("CurrentTime", elapsedTicks);
             PlayerPrefs.DeleteKey("CurrentTime");
             elapsedTicks -= ct;
             if (elapsedTicks < miliseconds)
             {
                 miliseconds -= elapsedTicks;
             }
             else
             {
                 miliseconds = 0;
             }
         }
         
         minutes = (int)miliseconds / 60;
         miliseconds -= (minutes * 60);
         
         seconds = (int)miliseconds;
         miliseconds -= seconds;
         
         PlayerPrefs.DeleteKey("TimeOnExit");
         
     }
     savedSeconds = 0;
 }
 
 public void Update()
 {
     // count down in seconds
     miliseconds += Time.deltaTime;
     if (resetTimer)
     {
         ResetTime();
     }
     if (miliseconds >= 1.0f)
     {
         miliseconds -= 1.0f;
         if ((seconds > 0) || (minutes > 0))
         {
             seconds--;
             if (seconds < 0)
             {
                 seconds = 59;
                 minutes--;
             }
         }
         else
         {
             resetTimer = allowTimerRestart;
         }
     }
     
     if (seconds != savedSeconds)
     {
         //  Show current time
         timer.text = string.Format("{0}:{1:D2}", minutes, seconds);
         savedSeconds = seconds;
     }
 }
 
 void ResetTime()
 {
     minutes = defaultStartMinutes;
     seconds = 0;
     savedSeconds = 0;
     miliseconds = 1.0f - Time.deltaTime;
     resetTimer = false;
 }
 
 private void OnApplicationQuit()
 {
     int numSeconds = ((minutes * 60) + seconds);
     if (numSeconds > 0)
     {
         miliseconds += numSeconds;
         PlayerPrefs.SetFloat("TimeOnExit", miliseconds);
         
         if (useElapsedTime)
         {
             int elapsedTicks = (int)(DateTime.Now.Ticks / tickPerSecond);
             PlayerPrefs.SetInt("CurrentTime", elapsedTicks);
         }
     }
 }
 
               }
Thank you :)
Answer by TBruce · Aug 04, 2016 at 11:34 PM
The easiest way would be to first add the following to your uses statements at the top of your timer script
 using UnityEngine.SceneManagement;
 
               next modify the Update() function in the timer script to look like this
 public void Update()
 {
     // if we are still counting down
     if (((minutes * 60) + seconds) > 0)
     {
         // count down in seconds
         miliseconds += Time.deltaTime;
         if (resetTimer)
         {
             ResetTime();
         }
         if (miliseconds >= 1.0f)
         {
             miliseconds -= 1.0f;
             if ((seconds > 0) || (minutes > 0))
             {
                 seconds--;
                 if (seconds < 0)
                 {
                     seconds = 59;
                     minutes--;
                 }
             }
             else
             {
                 resetTimer = allowTimerRestart;
             }
         }
     
         if (seconds != savedSeconds)
         {
             //  Show current time
             timer.text = string.Format("{0}:{1:D2}", minutes, seconds);
             savedSeconds = seconds;
         }
     }
     else
     {
         // reload the current scene
         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
     }
 }
 
               This essentially reloads the scene when the timer hits 0.
Thank you, I will try this out :) I just tried it out and I'm getting this error:
 error CS0234: The type or namespace name Scene$$anonymous$$anagement' does not exist in the namespace UnityEngine'. Are you missing an assembly reference? 
If I remember correctly Scene$$anonymous$$anagement was added in Unity 5.3.0. I am using Unity 5.3.2 and I know it works in this version.
ah okay thank you, I have the 5.1.2. I'll update :) thank you
Your answer
 
             Follow this Question
Related Questions
adding two integers together once every half second,adding two integers together with delay? 1 Answer
Countdown timer for starting game 0 Answers
I need a "loop" timer that disables a action until the timer resets. 0 Answers
Countdown timer in multiple scene? 1 Answer
slider for video player 0 Answers