Heavy Processing Messes With Time.time

Hi there,

I begin my game by starting a coroutine:

void Start() {
    StartCoroutine(ImportAssets());
}

In here, I wait for two seconds, and then save the current time:

IEnumerator ImportAssets() {
    yield return new WaitForSeconds(2);
    
    startTime = Time.time;
}

Then, in my Update function I test to see if a character needs importing, if it does I disable the script while it imports. (Ignore the first if statement for a moment.)

void Update() {
    if (itemsLoaded == total) {
        Helpers.Log(Time.time);
        Helpers.Log(startTime);
        Helpers.Log("Loaded in", Time.time - startTime);

        total = 0;

        enabled = false;
    }

    if (readyCharacters.Count > 0) {
        enabled = false;

        StartCoroutine(ImportCharacterObjects(readyCharacters[0]));
    }
}

Importing the characters takes a lot of work, and actually holds a few frames for a couple of seconds. Once all the characters are imported, that if statement runs fine, however Time.time is definitely incorrect.

  • startTime logs out at ~2.002099 (Which is correct)
  • Time.time logs out at ~4.877523 (Which is incorrect, using a stop watch I measure at least 8 seconds)
  • As such, Loaded in logs out at ~2.875424, which is obviously also incorrect.

I’ve had the thought that maybe locking up frames in the game throws out its time counter? Otherwise I’d love to find out what’s going wrong, as I need an accurate timer for this importing process.

Thanks in advance!

Time.time is not “real” time; it’s affected by things like Time.timeScale, and whether the game is paused. It thus seems probable that it is also affected by Time.maximumDeltaTime (which exists to prevent well of despair scenarios), meaning that frames longer than the maximum delta time get added as if they took the maximum time amount rather than the real, higher amount.

For the desired functionality, try experimenting with some of the other properties available in Time, such as Time.realtimeSinceStartup or Time.unscaledTime. If those don’t work, I would recommend using the System.Diagnostics.Stopwatch class.