- Home /
Question by
thetimmy997 · Jan 14, 2019 at 09:44 AM ·
timerstopping
Restart timer?
Noob question here. How can I reset the timer or initialize it back starting to zero after changing scenes, etc?
public Text counterText;
public float seconds, minutes;
// Initialization
void Start()
{
counterText = GetComponent<Text>() as Text;
}
// Update once per frame
void Update()
{
minutes = (int)(Time.time/60f);
seconds = (int)(Time.time % 60f);
counterText.text = minutes.ToString("00") + ":" + seconds.ToString("00");
}
Comment
Best Answer
Answer by Hellium · Jan 14, 2019 at 09:51 AM
public Text counterText;
public float seconds, minutes, startTime;
// Initialization
void Start()
{
counterText = GetComponent<Text>() as Text;
startTime = Time.time;
}
// Update once per frame
void Update()
{
minutes = (Time.time - startTime) / 60f;
seconds = (Time.time - startTime) % 60f;
counterText.text = string.Format( "{0:00}:{1:00}", minutes, seconds)
}
Answer by badadam · Jan 14, 2019 at 10:09 AM
Try this script.
public Text counterText;
public float seconds, minutes;
public float time;
// Initialization
void Start()
{
time = 0;
counterText = GetComponent<Text>() as Text;
}
// Update once per frame
void Update()
{
time += Time.deltaTime;
minutes = (int)(time / 60f);
seconds = (int)(time % 60f);
counterText.text = minutes.ToString("00") + ":" + seconds.ToString("00");
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612185728im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Timer-function which calculates how long an enabled script is running yet? 1 Answer
Change main Camera to another camera with a Trigger then back with a Timer. 1 Answer
I need some help modifying a script 2 Answers
Time.realtimeSinceStartup returns negative number 3 Answers
Self moving paddle 2 Answers