- Home /
Count down timer.
I have written the following code for count down timer but its not working.
var seconds : int = 60;
function start()
{
InvokeRepeating("CountDown",1.0,1.0);
}
function CountDown()
{
if(--seconds ==0)
{
CancelInvoke("CountDown");
}
}
function OnGUI()
{
GUI.Label(Rect(10,45,100,55), "Counter: " + seconds);
}
Answer by Kajos · May 27, 2013 at 01:39 PM
Make sure seconds is float and use Time.deltaTime (time per frame) or else you will get different results at different fps speeds.
Actually, since he is using InvokeRepeating(), the ti$$anonymous$$g will be pretty close. The InvokeRepeating() will not happen at exactly one second, but usually just a bit more. The average "bit more" will change with frame rate, but the ti$$anonymous$$g change with FPS will not be substantial.
Your answer
Follow this Question
Related Questions
GUI elements vanish when publishing 1 Answer
Timer Between Labels 2 Answers
Need some help an object collider that stops a timer then displays it on another scene 1 Answer
countdown/countup timer 1 Answer
Timer Pause 1 Answer