Hyrial
Hyrial

Reputation: 1888

Time doesn't correctly update in Unity Inspector

When I run the game, Unity's inspector updates every few seconds, but totalTimeElapsed only increments in a series of 0.1 seconds. I tried a similar thing with totalTimeElapsed += Time.deltaTime but with the same result.

How do I get the total time since game start?

float totalTimeELapsed = 0;
int dayCount = 0;

private void Update()
{
    totalTimeElapsed = Time.time;
    dayCount += 1;

    AFunctionTakingLotsOfTime();
}

Upvotes: 2

Views: 774

Answers (2)

Branden
Branden

Reputation: 347

float totalTime = 0f;

// Update is called once per frame
void Update () {

    totalTime += Time.deltaTime;
}

I believe what you are looking for would be something like this. Assuming this class is present on runtime of your game, you should have an accurate variable which can tell you the elapsed time since the game started.

You might also want to know that the update call occurs once per frame, ie. if you are running at 60 FPS update will be called 60 times in that second. You can try FixedUpdate which updates in real-time intervals instead.

Upvotes: 1

Because AFunctionTakingLotsOfTime() takes a long time, you need Time.realtimeSinceStartup

Time.realtimeSinceStartup deals in real time, rather than computed frame-rate-time amounts that are probably getting messed up due to the fact that your function takes a long time.

Alternatively you could use DateTime.

Note that both will not be affected by things like time scale and you may want to change your function to be less intensive, such as computing only a small chunk any given call (or by using a coroutine) or by splitting the method off onto a Job thread.

Upvotes: 2

Related Questions