- Home /
 
 
               Question by 
               unity_x6Zl-zvtB8Mp_g · Jun 09, 2021 at 06:13 PM · 
                timetimertime.deltatimetimer-scripttimers  
              
 
              Timer reset
Hey guys, I have a timer in my game which should count the time till the end of the game. I can't figure out how to make it so it will only reset back to 0 when menu scene is active. ( build index 0) The second problem is that I don't want it to go back to 0 after every death(scene reload). Have you got any sugestions?
using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class Timer : MonoBehaviour { public Text TimerText; public bool shouldCountTime; public float t;
 private void Start()
 {
     if (SceneManager.GetActiveScene().buildIndex != 0)
     {
         shouldCountTime = true;
     }
     else
     {
         shouldCountTime = false;
         t = 0;
     }
     t = Time.deltaTime;
 }
 void Update()
 {
     if (shouldCountTime) {
         t += Time.deltaTime;
     }
     string minutes = ((int)t / 60).ToString();
     string seconds = (t % 60).ToString("f2");
     TimerText.text = minutes + ":" + seconds;
 } 
 
               }
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by Mrpxl · Jun 10, 2021 at 09:59 AM
What I can suggest here would be to use a static float so it will stay in memory even when reloading a scene (and its script).
Your answer