Unity 2D: Timer stops when game over panel is shown

I'm having trouble with starting my timer when my player loses all of his hearts (lives). You see, when player loses all of his lives and the game over panel slides down the timer just stops at 4:59 and doesn't count down, just stays at 4:59.My heirarchy looks like this

This is my timer 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; private bool resetTimer = false;  private DateTime centuryBegin = new DateTime(2001, 1, 1);  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 / 10000000);             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 / 10000000);             PlayerPrefs.SetInt("CurrentTime", elapsedTicks);         }     } } 

}

And this is my player's health script:

public int curHealth; public int maxHealth = 3; Vector3 startPosition; public PlayerHealth playerhealthRef; public TimeManager countDownTimer; // change this to your class name float counter; public Animator anima; // drag the panel in here again private UI_ManagerScripts UIM; private PlayerScript ps; DateTime currentDate; DateTime oldDate;   void Start () {     curHealth = maxHealth;     startPosition = transform.position;     ps = GameObject.FindGameObjectWithTag("PlayerScript").GetComponent <PlayerScript> ();     currentDate = System.DateTime.Now;     if (countDownTimer != null)     {         countDownTimer.gameObject.SetActive(false);     } }   void Update () {      if (curHealth > maxHealth) {         curHealth = maxHealth;     }     if (curHealth <= 0) {          Die ();     } }  void Awake() {     UIM = GameObject.Find ("UIManager").GetComponent<UI_ManagerScripts> (); }  void Die(){      if (PlayerPrefs.HasKey ("Highscore")) {         if (PlayerPrefs.GetInt ("Highscore") < ps.Score) {             PlayerPrefs.SetInt ("Highscore", ps.Score);         }     } else      {         PlayerPrefs.SetInt ("Highscore", ps.Score);     }      UIM.EnableBoolAnimator(anima);     PlayerPrefs.SetInt("RemainingLives", curHealth);     PlayerPrefs.Save();      if (countDownTimer != null)     {         countDownTimer.Enable(true);     }  }  void OnApplicationQuit() {     PlayerPrefs.SetString("sysString", System.DateTime.Now.ToBinary().ToString());     print("Saving this date to prefs: " + System.DateTime.Now); }  void LoadSaveData() {     PlayerPrefs.GetInt ("RemainingLives",0);     OnApplicationQuit (); }  public void Damage(int dmg) {     curHealth -= dmg;     Reset(); }  void Reset () {     transform.position = startPosition;     GotoMouse.target = startPosition;  }    

}

Replay

Category: unity3d Time: 2016-07-30 Views: 0
Tags: timer unity3d

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.171 (s). 12 q(s)