# Time.deltaTime timer counting slowly

We are making a simple game which is somewhat based around going quickly and we wanted a timer to tell the player how long they took to beat the game.
The timer worked for a very long time but recently it started counting up slower than actual seconds and we don’t know why.
Everyone who has talked about making timers uses the method that we used (Time.deltaTime) and it shouldn’t have any issues. If anyone knows what could be happening that would be great.
public class Timer : MonoBehaviour
{
private float time = 0f;
private float time2 = 0f;
public bool alive = true;
public Text timer;

``````private void Update()
{
if (alive) {
time += Time.deltaTime;
if (time >= 59.5) {
time2 ++;
time = 0;
}

}
if (time < 9.5)
{
timer.text = "Timer:
``````

" + time2 + “:0” + (int)(time+0.5f);
}
else
{
timer.text = "Timer:
" + time2 + “:” +(int)(time + 0.5f);

``````    }
}
``````

}

@NaturalTenacity, there is a tactical coding difference between using a timer as a basis for motion or event selection, and simply needing to get an elapsed time.

It sounds like you want the latter, and the code you have is a complex way of doing it with some inaccuracies built in. There is much, much, simpler way.

Get the start time at the beginning of the game, the end time at the end, and subtract the two to get the elapsed time, which will be in seconds. Format as needed for display:

``````private float startTime;
private void Start( ) {
startTime = Time.time;
}

private void GameOver() {
float elapsedTime = Time.time - startTime;
string minutes = Mathf.Floor( elapsedTime / 60 ).ToString( "00" );
string seconds = Mathf.Floor( elapsedTime % 60 ).ToString( "00" );
timer.text = "Timer:
``````

" + minutes + “:” + seconds;

`````` }
``````

Or if you want the running timer displayed then dispense with the above and use

``````private void Update() {
if (alive) {
currentTime += Time.deltaTime;
string minutes = Mathf.Floor( currentTime / 60 ).ToString( "00" );
string seconds = Mathf.Floor( currentTime % 60 ).ToString( "00" );
timer.text = "Timer:
``````

" + minutes + “:” + seconds;
}
}

I don’t see any major issues with your code besides that I would strongly recommend to rename “time” to “seconds” and “time2” to “minutes”. Why using abstract names? Variable names should be as descriptive as possible.

The next thing is you should change the type of time2 from float to int. You only count the minutes in whole minutes. Using a float will just call for trouble at higher numbers.

Next your roll-over between your seconds and minutes should be done like this:

``````float seconds = 0;
int minutes = 0;

// [ ... ]
seconds += Time.deltaTime;
if (seconds >= 60f)
{
seconds -= 60f;
minutes++;
}
timer.text = "Timer:
``````

" + minutes.ToString(“00”) + “:” + ((int)seconds).ToString(“00”);

The difference is that we don’t loose any fractional time components. When we reach 60 seconds we simply exchange 60 seconds into 1 minute. That way the timer should be as accurate as possible.

Floating point numbers are a bit tricky. Have a look at my precision table over here and maybe have a look at Tom Scott’s video on floating point numbers

Apart from all that there’s no reason why the timer in your code should run slower than normal. If anything it would be slightly faster since you skip about half a second every minute. Of course such a timer would never be in perfect sync with the realworld since even the smallest inaccuracies would accumulate over time. When you said it “runs slower”, did you actually measure how much slower over what time period?

try using Time.unscaledDeltaTime;
You might’ve changed the timescale in some other script.
Do this to ensure it doesn’t mess up this timer.