OnGUI and Time.deltaTime

My first question: Is it allowed to use the Time class in the OnGUI method?

I am asking this because I found some strange behaviour using it. Basicly I am animating something in the OnGUI method, using the Time.deltaTime to make the animation framerate independent. Nothing strange about that. I noticed that the animation actually runs twice as fast as it logically should run!

So I went out and checked it. I found that Time.deltaTime is different that when I calculate it myself by saving the old Time.time and substract it with the new Time.time!

When I use the self calculated deltaTime the animation runs as aspected. (yes, I did sat down with a stopwatch measuring :))

Its important to I am using the Time class inside my OnGUI because it will draw a custom made widget, there is no Update method in its scope.

I’ll file a bugreport but I hope someone can give me some explaination, as maybe its just me making a mistake. I think has to do with OnGUI() and Update() not running synchroon.

If you want a small example, run this little C# script and check the log.

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour
{
    void OnGUI()
    {
       TestDeltaTime();
    }

    private static float oldTime = 0;

    public static void TestDeltaTime()
    {
        float delta = Time.time - oldTime;

        Debug.Log("unity " + Time.deltaTime);
        Debug.Log("mine " + delta);

        oldTime = Time.time;
    }
}

Depending on how you structured your usage of delta time, this is possible. Understand that OnGUI is executed multiple times per frame. So while you were probably calculating the proper delta from the previous frame, your animation was being stepped multiple times per frame with that delta value. Thus, causing your animation to run twice as fast.

I just checked and this isn’t mentioned in the official documentation for the function, it should be.

If you want to update something basing on the world simulation update from Unity, you must either use Update or FixedUpdate as their name implies :slight_smile:

Right, I did suspect OnGUI doesn’t run synchronised with Update but I could not find anything about on the forum or in the docs. Although it is a bit weird that the engine can’t handle this since it does know when Time.deltaTime is used in Update or FixedUpdate.

Dreamora, as I am building a widget in the same style as the GUI class I cannot use the Update function. Its the same like GUI.Button but then animated.

It is working now, I was just trying to understand the engine innerworkings.

it runs syncronized with update, kind of at least. As mentioned above, it runs more than once per frame (at least twice, once to render the gui and once to handle the interaction)
As such your delta code will fail because on the second call, Time.Time might still be the same.

Time.deltaTime will always be correct as it is updated once per simulation run and holds the difference required for the last simulation run.

Thats some nice information, didn’t knew about that. My deltaTime actually does work i believe, 1 out of 2 times it returns 0. Now I know why :slight_smile: