DeltaTime Timer wont increment when game is focused

Hello, i have been making a double timer in Unity, the timer works perfectly in editor

But when the game is built, when i open my game, it will either increment super slowly or it wont at all, but if i am at windowed mode, if i hold left mouse on the window of my game i can see the timer counting without problems, so why would Time.DeltaTime not work when the game is focused? I can’t decide if this is a bug or not so that’s why i came here.

using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class timertest : MonoBehaviour {

    public Text Timer;
    public double time;

	// Use this for initialization
	void Start () {

        time = Time.time;
	}
	
	// Update is called once per frame
	void Update () {


        time = Math.Round(time + Time.deltaTime, 2);

        Timer.text = time.ToString();
    }
}

As it can be seen here there’s nothing particularly problematic about the code either, yet it’s not working when building and i have no idea why, any help would be appreciated.

I’m not sure if your timer not working at all or too slow or runs at irregular speed, but I’ve seen this kind of problems lots of time.

If you replace time = Math.Round(time + Time.deltaTime, 2); by time = time + Time.deltaTime;, your timer should work properly and while you’ll end up with a correct number, it’s ugly. We’ll make it pretty later.

If your bug is not fixed, … I don’t know what is happening. Try Time.realDeltaTime or something like that, or just time = Time.time. Or maybe your timertest script is no longer attached to a game object or it’s disabled or desdtroyed.

If your timer is working, then it’s time to make it look good. Replace Timer.text = time.ToString(); with Timer.text = Math.Round(time, 2).ToString();.

Ok, but I just move Math.Round1 line down, why?

You have 2 different kind of variables here. I’m not talking about data types like double and Text, but how you use the variables.

  • The time variable is used as an internal variable, it is used to keep track of stuff, here the the time.
  • The Timervariable is used as an external variable, it is used to display information to the user. Displaying big ugly numbers in an “external” variable is not good, so round off the values when, and olny when updating this kind of variable.

What is really happening in your code, let’s say your game is running at a good and stable 60 fps (1/60 = 0.0166666667):

  • in Start(): time = 123.1
  • in 1st Update(): time = Math.Round(123.1 + 0.0166666667, 2) = Math.Round(123.116667) = 123.12
  • in 2nd Update(): time = Math.Round(123.12 + 0.0166666667, 2) = Math.Round(123.136667) = 123.14

It’s possible that the timer might work for a few seconds, then stop because the rounded and unrounded numberrs are really similar. If your FPS is way higher, the timer may not work at all.

TL;DR only round numbers at the end of a calculation, naver on a running total and only do it when displaying the value on screen.

Okay, i have found the problem

It was that i was constantly rounding Time.DeltaTime

So how did i fix this?

Well it’s simple, instead of rounding Time.DeltaTime every frame, i formatted it instead

	void Update () {

        time = time + Time.deltaTime;

        Timer.text = time.ToString("0.00");
    }