- Home /
Timer script not perfectly working
Hi guys, I have a script that makes a countdown timer but I have a problem with it. Here's my code: Variable declaration:
public float Seconds;
public float Minutes;
In Update():
if(Seconds <= 0){
Seconds = 59;
if(Minutes >= 1){
Minutes --;
}
else{
Minutes = 0;
Seconds = 0;
}
}
else{
Seconds -= Time.deltaTime;
if(Mathf.Round (Seconds) <= 9){
timerText.text = timerText.text = "Mission time: " + Minutes.ToString () + ":0" + Seconds.ToString("f1");
}
else{
timerText.text = timerText.text = "Mission time: " + Minutes.ToString () + ":" + Seconds.ToString("f1");
}
}
Now, my problem with this code is the transitions. When it reaches 1:01 instead of going to 0:59 it goes from 1:00 to 0:59 to 0:58 really fast since it does the 1:00 for half a second then 0:59 the other half.
Also, when it reaches 0:10, instead of going directly to 0:09.xx it goes 0:9.xx for a few milliseconds.
Anyone can help me fix those two issues? Or is my code just to scrap and redo anew for what I want it to do?
Well I got my problem almost solved for the 0:10 to 0:09 but it still takes a frame to switch and it's visible... Any way to make it instant?
I changed if($$anonymous$$athf.Round (Seconds) <= 9) to if(Seconds <= 9.99f)
Still getting that frame where it switch :s but that's going to have to do it...
But the problem is still in the $$anonymous$$ute decremental. That takes place on at least half a second.
I think this may have more to do with the fact that Update is unpredicably called. As in, Update is arbitrarily called as frequently as possible. Have you tried using FixedUpdate?
Answer by zharik86 · Mar 14, 2014 at 08:41 PM
I will write for you a small example of the timer. The main thing - attention to the string.Format function:
public float min = 1; //other value
public float sec = 25; //value from 0 to 59
private string texttimer = "";
void Start() {
texttimer = string.Format("{0:00}:{1:00}", min, sec);
//here, for example, 0:00, where 0 - number value of list argument - min
//and 00 - count sign of value
}
void Update() {
sec = sec - Time.deltaTime;
if(sec < 0) {
min = min - 1.0f;
sec = 59.0f;
}
if(min >= 0) { //time is over?
texttimer = string.Format("{0:00}:{1:00}", min, sec);
}
}
That's good but it still does the same thing :s
Is there no way in unity to have a perfect timer that properly changes from $$anonymous$$utes to seconds or is it that I put the text update at the wrong place? I put it right after with a Debug.Log(texttimer);
@Jonathan07 Not really clearly that at you happens to transitions. Probably, it is worth trying a little changed option. In it I do counting only on seconds so it shows the true change in seconds:
public int $$anonymous$$ = 1; //value in $$anonymous$$ute
public int sec = 25; //value in sec
private float fullsec = 0.0f;
private string texttimer = "";
private string texttimersec = "";
void Start() {
fullsec = 60.0f * $$anonymous$$ + sec;
texttimer = string.Format("{0:00}:{1:00}", $$anonymous$$, sec);
texttimersec = string.Format("{0:00}", fullsec);
}
void Update() {
fullsec = fullsec - Time.deltaTime;
if(fullsec >= 0) { //time is over?
int my$$anonymous$$in = (int)(fullsec/60.0f); //current $$anonymous$$ute
texttimer = string.Format("{0:00}:{1:00}", my$$anonymous$$in, fullsec - my$$anonymous$$in * 60.0f);
texttimersec = string.Format("{0:00}", fullsec);
}
}
But you remember that the string.Format function rounds all values according to rules of mathematics. I.e. value 0.45 is rounded as 0, and value 0.65 is rounded as 1. If you don't want to use rounding, it is possible to discard values after a comma. Thus, and for seconds it is possible to use the int type. However, there is one more method. This creation of function and call of this function through a certain interval of time, in your case 1 second. If the example is necessary, write.
Your answer
Follow this Question
Related Questions
Simple Timer 2 Answers
Calling methods 0 Answers
Timer doesn't work properly 2 Answers
When Score goes up by 25 change timer value 1 Answer
Countdown Through a Label? 1 Answer