Unscaled Time VS Real-Time Since Startup in Unity
When your game development process involves dealing with time in the game, you may have come across these time-related properties called Unscaled Time and Real-Time Since Startup, which both seem to do the same thing. But are they really the same?
Unscaled Time and Real-Time Since Startup’s behaviors are, for the most part, the same. Both of these properties are the time in seconds since an application started, with the difference being: The Unscaled Time value will stay constant when called multiple times within a frame while the Real-Time Since Startup value can change.
These two properties are mostly the same. Both of them ignore the time scale value and give you the time since the game started. Below are some comparisons between Unscaled Time and Real-Time Since Startup when used in various situations.
The Differences Between Unscaled Time And Real-Time Since Startup in Unity
Let’s see how Unscaled Time (Time.unscaledTime) and Real-Time Since Startup (Time.realtimeSinceStartup) behave in various situations.
The values are slightly different
Even though the definition for these two properties are pretty much the same which is “the real-time since the game started,” there is a slight difference between them.
Take a look at the output of this script below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TimeTest : MonoBehaviour
{
void Update()
{
Debug.Log("Time.unscaledTime: " + Time.unscaledTime);
Debug.Log("Time.realtimeSinceStartup: " + Time.realtimeSinceStartup);
Debug.Log("----------------- FRAME ENDED ------------------");
}
}
Code language: C# (cs)
This will output the values of Unscaled Time (Time.unscaledTime) and Real-Time Since Startup (Time.realtimeSinceStartup) every frame.
Here is the output:
You’ll notice that the values are not the same even though both of them were called in the same frame, every frame.
Why is that? Shouldn’t both of them output the same value every time?
The reason these 2 properties output different values while being in the same frame is due to Time.unscaledTime actually returns the time at the start of a frame, while Time.realtimeSinceStartup returns the time at the moment it is being called.
When being called multiple times within a frame
Another difference between these two is how the values change when called multiple times within a frame.
As stated earlier, the Time.unscaledTime value is actually the time at the start of a frame. This means its value will stay the same no matter how many times you call it inside a frame.
On the other hand, the Time.realtimeSinceStartup value can be different each time it is called within a frame.
Take a look at this script below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TimeTest : MonoBehaviour
{
void Update()
{
Debug.Log("Time.unscaledTime: " + Time.unscaledTime);
Debug.Log("Time.unscaledTime: " + Time.unscaledTime);
Debug.Log("Time.unscaledTime: " + Time.unscaledTime);
Debug.Log("Time.realtimeSinceStartup: " + Time.realtimeSinceStartup);
Debug.Log("Time.realtimeSinceStartup: " + Time.realtimeSinceStartup);
Debug.Log("Time.realtimeSinceStartup: " + Time.realtimeSinceStartup);
Debug.Log("----------------- FRAME ENDED ------------------");
}
}
Code language: C# (cs)
In the example code above, we call both Time.unscaledTime and Time.realtimeSinceStartup 3 times each within a frame.
Here is the output in the Unity console:
As you can see here, the value of Time.unscaledTime remains unchanged no matter how many times you call it within a frame, while Time.realtimeSinceStartup can return different values because it always returns the current time reported by the system timer.
They both ignore the time scale value
This should be obvious but, unlike scaled time value, both Time.unscaledTime and Time.realtimeSinceStartup always ignore the time scale value no matter what.
Set Time.timeScale to 0 and see what happens:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TimeTest : MonoBehaviour
{
private void Start()
{
Time.timeScale = 0;
}
private void Update()
{
Debug.Log("Time.timeScale: " + Time.timeScale);
Debug.Log("Time.unscaledTime: " + Time.unscaledTime);
Debug.Log("Time.realtimeSinceStartup: " + Time.realtimeSinceStartup);
Debug.Log("----------------- FRAME ENDED ------------------");
}
}
Code language: C# (cs)
In the example above, we set Time.timeScale to 0 and output the values of Time.unscaledTime and Time.realtimeSinceStartup every frame.
As you can see, the values of both Time.unscaledTime and Time.realtimeSinceStartup continues to change every frame regardless of the time scale value.
Wait instructions in coroutines don’t affect the values
Because of how the coroutine work, Time.unscaledTime and Time.realtimeSinceStartup are unaffected by the wait instructions.
This is due to how a coroutine can run over multiple frames. As long as the frame advances, Time.unscaledTime will update its value.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TimeTest : MonoBehaviour
{
private void Start()
{
StartCoroutine(TimePausing());
}
IEnumerator TimePausing()
{
yield return new WaitForSeconds(1f);
Debug.Log("===== Before Yield =====");
Debug.Log("Time.unscaledTime: " + Time.unscaledTime);
Debug.Log("Time.realtimeSinceStartup: " + Time.realtimeSinceStartup);
yield return new WaitForSeconds(3f);
Debug.Log("===== After Yield =====");
Debug.Log("Time.unscaledTime: " + Time.unscaledTime);
Debug.Log("Time.realtimeSinceStartup: " + Time.realtimeSinceStartup);
}
}
Code language: C# (cs)
The coroutine in the example above outputs the values of Time.unscaledTime and Time.realtimeSinceStartup, waits for 3 seconds and then outputs the values again.
Conclusion
Both Time.unscaledTime and Time.realtimeSinceStartup are mostly the same, with a bit of a difference in how Time.unscaledTime only outputs the time at the start of a frame, while Time.realtimeSinceStartup always outputs the current time based on the system clock.
I hope you learned something new from this article. Have a nice day!
Cheers!
Attribution
Clock icon made by Freepik from www.flaticon.com