- Home /
Weird Problem with Clock
I am making a clock to work with my sun, and I have just lost my patients. I can not think of any reason why this script won't work. What happens is that when the seconds hit 60, they set to 0 and stay there. Then the minutes start going up at light speed, resetting themselves the way they should, and then making the hours go up the way they should. I don't have an idea what is going on, but I know that it aint right.
This is the basic clock system:
second = Time.time;
if(second >= 60)
{
second = 0;
minute ++;
if(minute >= 60)
{
minute = 0;
hour ++;
if(hour >= 24)
{
hour = 0;
}
}
}
Answer by HenryStrattonFW · Mar 02, 2013 at 07:52 PM
This is because you are using Time.time. When what you want to use is Time.deltaTime (delta time is the change in time since the last update) you want to add that to your seconds (your missing a + before the = as well but that may just be a typo).
See Time.time stores the time since the app started. which once over a minute, will always be setting your second value to greater than 60 each update.
Answer by $$anonymous$$ · Mar 02, 2013 at 07:50 PM
this isn't a glitch, once the seconds are >= 60, you reset the variable second, witch is instantly changed back to Time.time when you call second = Time.time;
you need to increment the seconds by one when 1 seconds passes, not directly set seconds like this: Replace second = Time.time; with
`
//c#
void Start () { InvokeRepeating("Increment", 1, 1); }
void Increment () { second += 1; }
Answer by Eric5h5 · Mar 02, 2013 at 07:51 PM
Think about it...Time.time is a count in seconds since the game started, so when it gets to 60 and higher, your first if statement will always be true, so you will always set "second" to 0 and always increment "minute" by one and so on. When you reset "second" to 0, that has no effect on Time.time, which will continue counting up forever.
If you want to use this particular method, you should not use Time.time, but rather increment "second" by itself. I'd recommend using InvokeRepeating with an interval of 1.0, so you can have "second" be an integer that's incremented by one every time the function is called. If you're going to use Update, then make "second" a float where you add Time.deltaTime to it every frame. (But in that case, you'd want to do "`second -= 1.0`" instead of "`second = 0`", in the interests of accuracy.)
Your answer
![](https://koobas.hobune.stream/wayback/20220613093708im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Minute Timer Issue 1 Answer
Visual timer? Like CUT THE ROPE 2 Answers
incrementing instead of speeding up ingame clock 2 Answers
weird texture distortion 0 Answers
Mac webplayer graphic glitch 1 Answer