Unity Timer.deltaTime is consistently inaccurate (reporting higher number),Time.deltaTime reports incorrect (larger) number
Hello,
I have a prefab with the following script:
public class RefineryScript : MonoBehaviour {
float timer = 0f;
public float loopRate = 5f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
timer += Time.deltaTime;
if (timer > loopRate)
{
Debug.Log("Timer was " + timer);
timer = 0f;
}
}
It is supposed to log every 5 seconds. However, it is logging every 3 seconds. Watch the actual log time in the picture.
I tried also to implement this in a Coroutine which yields WaitForSecondsRealtime(5f)
, I also get 3 seconds time delay instead of 5.
Any ideas?
$$anonymous$$r. @NoDumbQuestion. The link has nothing to do with this. I am running this on Update, not FixedUpdate, which should be called once a second.
Answer by makketronics · Sep 08, 2018 at 04:58 PM
I counted how many frames in 5 seconds, divided 5 by that number. I get around 300 (302, 298) within 5 seconds.
This means that the Timer.deltaTime "thinks" it is at 60 fps. However, in the stats, I am running around 111fps.
Edit: As per comments below, the issue is with timeScale. Setting timeScale to 1f or using unscaledDeltaTime solved the problem.
I think you just dont have enough deep understand how Update/FixedUpdate core work within Unity.
Update() is called on FrameRate. Coroutine is called and checked also on Update(). Anything on Framerate will always be fluctuate so dont expecte every frame called the same time. And turn off vsync if you want editor to think it can run above 60 FPS. So if you want precise + consistency deltatime. Then use FixedUpdate() to debug time.
@NoDumpQuestion, Thanks for your reply. However, you are not completely understanding the problem. I am aware that Update() fluctuates. I am simply integrating deltaTime to get total time. For example, if I wait for 10 seconds, I can simply count: 5 + 3 + 3 = 11. In this case, deltaTime is 5, 3, 3. However, Unity is telling me 5, 3, 3 when it should've been 4, 2, or so. Did you look at the log and compare Unity's accumulated time vs actual log time?
Then you forgot to count unscaledDeltaTime.
Your answer
![](https://koobas.hobune.stream/wayback/20220612174248im_/https://answers.unity.com/themes/thub/images/avi.jpg)