Unity Ninja Unity 3D Forum
To have exclusive access to all downloads, recourses and links within this forum, a free registered account is required. Just a heads up Wink

You are not connected. Please login or register

Unity Ninja Unity 3D Forum » HELP » Help (General) » Unity2D: Timer Freezes when player health loses all of his lives

Unity2D: Timer Freezes when player health loses all of his lives

View previous topic View next topic Go down  Message [Page 1 of 1]

wasicool7


Member [Lv. 0]
Member [Lv. 0]
I'm having problem with my timer countdown. The timer script is activated once my player loses all of his hearts and the game over panel is shown. My problem is that the timer freezes and stops counting down when the player loses all of his hearts and the game over panel is shown.

Thank you!  Smile

This is my Time manager script:

       
Code:
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:


Code:


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 GameOverManager UIM;
 DateTime currentDate;
 DateTime oldDate;
 
 
 void Start ()
 {
 curHealth = maxHealth;
 startPosition = transform.position;
 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<GameOverManager> ();
 }
 
 void Die(){
 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;
 PlayerMovement.target = startPosition;
 
  }
       }

View user profile

jakeyiii


Member [Lv. 3]
Member [Lv. 3]
Hello

We see that you have some issues with your project/script, however we can help you for a price. If you would like us to work for you then please feel free to contact us at http://RenntekStudios.co.uk or on Skype at RenntekStudios thanks

Our prices are very cheap. Little errors we can fix for free, otherwise projects we can discuss prices. Hope you get in touch shortly thank you...

View user profile

Pr0totype2


Member [Lv. 3]
Member [Lv. 3]
Try to print Time.timeScale every second and check if it continues to count after you died

View user profile

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum