Legacy Documentation: Version 4.6(go to latest)
Language: English
  • C#
  • JS
  • Boo

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Time.realtimeSinceStartup

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public static var realtimeSinceStartup: float;
public static float realtimeSinceStartup;
public static realtimeSinceStartup as float

Description

The real time in seconds since the game started (Read Only).

In almost all cases you can and should use Time.time instead.

realtimeSinceStartup returns the time since startup, not affected by Time.timeScale. realtimeSinceStartup also keeps increasing while the player is paused (in the background). Using realtimeSinceStartup is useful when you want to pause the game by setting Time.timeScale to zero, but still want to be able to measure time somehow.

Note that realtimeSinceStartup returns time as reported by system timer. Depending on the platform and the hardware, it may report the same time even in several consecutive frames. If you're dividing something by time difference, take this into account (time difference may become zero!).

	// Prints the real time since start up.

print(Time.realtimeSinceStartup);
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Example() {
        print(Time.realtimeSinceStartup);
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	def Example() as void:
		print(Time.realtimeSinceStartup)

Another example:

	// A FPS counter.
	// It calculates frames/second over each updateInterval,
	// so the display does not keep changing wildly.
	 
	var updateInterval = 0.5;
	private var lastInterval : double; // Last interval end time
	private var frames = 0; // Frames over current interval
	private var fps : float; // Current FPS

function Start() { lastInterval = Time.realtimeSinceStartup; frames = 0; }

function OnGUI () { // Display label with two fractional digits GUILayout.Label("" + fps.ToString("f2")); }

function Update() { ++frames; var timeNow = Time.realtimeSinceStartup; if( timeNow > lastInterval + updateInterval ) { fps = frames / (timeNow - lastInterval); frames = 0; lastInterval = timeNow; } }
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public float updateInterval = 0.5F;
    private double lastInterval;
    private int frames = 0;
    private float fps;
    void Start() {
        lastInterval = Time.realtimeSinceStartup;
        frames = 0;
    }
    void OnGUI() {
        GUILayout.Label("" + fps.ToString("f2"));
    }
    void Update() {
        ++frames;
        float timeNow = Time.realtimeSinceStartup;
        if (timeNow > lastInterval + updateInterval) {
            fps = frames / (timeNow - lastInterval);
            frames = 0;
            lastInterval = timeNow;
        }
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	public updateInterval as float = 0.5F

	private lastInterval as double

	private frames as int = 0

	private fps as float

	def Start() as void:
		lastInterval = Time.realtimeSinceStartup
		frames = 0

	def OnGUI() as void:
		GUILayout.Label(('' + fps.ToString('f2')))

	def Update() as void:
		++frames
		timeNow as float = Time.realtimeSinceStartup
		if timeNow > (lastInterval + updateInterval):
			fps = (frames / (timeNow - lastInterval))
			frames = 0
			lastInterval = timeNow