- Home /
Question by
Eco-Editor · Jun 22, 2017 at 10:21 AM ·
uitexttimerresettimer countdown
Timer question
Hello all
My timer is not showing seconds while counting down but rather it's starts from 99, 98, 97...
another thing, how can I reset it when going to another enabled room?
Please see picture
Here's the C# code
[RequireComponent(typeof(Text))]
public class Timer: MonoBehaviour {
//public static float timer;
Text timerText;
private float startTime = 300f;
private void Start()
{
timerText = GetComponent<Text>();
}
private void Update()
{
startTime -= Time.deltaTime;
string minuts = (startTime / 60).ToString();
string seconds = (startTime % 60).ToString("f0");
timerText.text = string.Format("{0:00} : {1:00}", minuts, seconds);
}
timecount.jpg
(34.9 kB)
Comment
Best Answer
Answer by Blue-Cut · Jun 22, 2017 at 04:20 PM
Hello,
I hate string formating, I am never able to make it work :(
This is my way to do what you want, I use the TimeSpan
class :
void Update()
{
startTime -= Time.deltaTime;
TimeSpan span = TimeSpan.FromSeconds(startTime);
timerText.text = span.Minutes + " : " + span.Seconds;
}