Time Counter
Hi, can anyone give me please a time counter script? I have one but it don't works how i want. I want to be hours/minutes/seconds(ex: 01:07:25.43). The problem is that i want the timer to activate only when i press a button, and when the button is pressed again the timer will start from where it remained.
Here is my script:
public Text timerText;
private float startTime;
// Use this for initialization
void Start () {
startTime = Time.time;
}
// Update is called once per frame
void Update () {
float t = Time.time - startTime;
string hours = ((int)t / 3600).ToString ();
string minutes = ((int)t / 60).ToString ();
string seconds = (t % 60).ToString ("f2");
timerText.text = hours + ":" + minutes + ":" + seconds;
}
}
Answer by Mmmpies · May 26, 2016 at 02:26 PM
You haven't reduced t by the hours when you calculate the minutes. So you'll get correct number of hours the the full time for minutes.
Try
string hours = ((int)t / 3600).ToString ("00");
float m = t % 3600;
string minutes = ((int)m / 60).ToString ("00");
string seconds = (m % 60).ToString ("00");
Not got access to Unity ATM so can't test it - sorry.
EDIT - for some reason the format screwed up on the code so I've put it as an answer for now.
It worked, thanks. But here is the real problem for me: I want the timer to stop and play when i press a button. I tried to put If(Input.GetButtonDown... but it still plays.
Well it depends what you're doing. In menus I set Time.timeScale to 0 but I honestly can't remember how that impacts on Time.time. Like I said I haven't got access to Unity so can't test it but try setting
Time.timeScale = 0.0f;
When the button's pressed.
EDIT Or it might be more appropriate to have t constantly updated by adding Time.deltaTime when you want the counter updating and not when you don't.
t += Time.deltaTime;
In Update as long as that button's not pressed.
When the timer reaches 1 $$anonymous$$ute tho it shows 60:00 for a fraction of second before turn in 00:59
If you want to avoid that use this ins$$anonymous$$d:
void Update()
{
CalculateTime();
}
private void CalculateTime()
{
// Stop the timer when it reaches 0
if (currTime <= 0)
return;
currTime = startTime - Time.time;
float m = currTime % 3600;
System.TimeSpan timeSpan = new System.TimeSpan((int)currTime / 3600), (int)m / 60, (int)m % 60);
timerText.text = timeSpan.ToString(@"hh\:mm\:ss");
}
This I believe is cleaner too :)
Answer by Toon_Werawat · May 26, 2016 at 02:49 PM
If you want to convert time to string format. Why not use TimeSpan? It way more easy!
void Update()
{
System.TimeSpan result = System.TimeSpan.FromSeconds(startTime);
System.DateTime actualResult = System.DateTime.MinValue.Add(result);
timerText.text = actualResult.ToString("hh:mm:ss");
//Or if you want old way.
timerText.text = result.Hours + ":" + result.Minutes + ":" + result.Seconds;
}
But at first I see you want time counter. You can just add bool variable for it.
public bool timeTick;
Then at update. If you press key. You can just do this.
void Update()
{
timeTick = Input.GetKey(Keycode.F);
//If it true
if (timeTick)
{
startTime = Time.time;
}
}
Doesn't work. Your string format not valid. Need something like @"hh\:mm\:ss" or "c"
Answer by Basakot · May 26, 2016 at 03:26 PM
public Text timerText;
private float startTime;
// Use this for initialization
void Start () {
startTime = Time.time;
}
// Update is called once per frame
void Update () {
float t = Time.time - startTime;
string hours = ((int)t / 3600).ToString ("00");
float m = t % 3600;
string minutes = ((int)m / 60).ToString ("00");
string seconds = (m % 60).ToString ("f2");
timerText.text = hours + ":" + minutes + ":" + seconds;
if (Input.GetButtonDown("Right")) {
startTime = Time.time;
}
}
}
@Toon_Werawat @Mmmpies this is how the scrip ended. It's exactly what i needed, but when i press the button, it starts from 0. I want to start from where it left.
startTime = Time.time;
Is fine for the first time you call it but really you need
startTime = t;
In that button down and it'll pick up the last value of t and not the current Time.time. Struggling to run this in my head but you may need to protect t from being updated all the time as well. startTime can be set to t but the
t = Time.time - startTime;
line runs all the time and Time.time will update. That idea of adding Time.deltaTime to t if the buttons not pressed would work though.
EDIT - in fact try this...
void Update () {
if (Input.GetButtonDown("Right")) {
// Do what ever you want in here or convert the if to an if not
} else {
float t += Time.deltaTime;
}
string hours = ((int)t / 3600).ToString ("00");
float m = t % 3600;
string $$anonymous$$utes = ((int)m / 60).ToString ("00");
string seconds = (m % 60).ToString ("f2");
timerText.text = hours + ":" + $$anonymous$$utes + ":" + seconds;
}
@$$anonymous$$mmpies Thanks, i just putted startTime = t ins$$anonymous$$d on the update startTime = Time.time;. Thanks so much again!
Hey no problem @Basakot amazed my "debug in head" worked. Technically I think adding Time.deltaTime is a better solution but really most program$$anonymous$$g, game or otherwise, is smoke and mirrors to make it look like the real world so if it "works as intended"™ then it's good enough ;¬)
Answer by komal2992 · Jan 17, 2018 at 07:20 AM
Guys it's very simple. Try this one
CountDown In hours, Minutes and seconds
countDown -= Time.deltaTime;
countDownText.text = (((Mathf.Floor(countDown / 3600f)) % 60).ToString("00")) + ":" + (((Mathf.Floor(countDown / 60f)) % 60).ToString("00")) + ":" + (Mathf.Floor(countDown % 60f).ToString("00"));
Your answer
![](https://koobas.hobune.stream/wayback/20220612081627im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Start and Stop Timer Help 1 Answer
I want player to be able to play again after 1 hour.,I 0 Answers
How do I add my Timer to my Score system in C# ? 0 Answers
Countdown timer... 2 Answers