Time.time explanation in Unity with example

Hello programmers, In this article, you will learn about Time.time in Unity with the help of an example in C#.

Before we get started with the building process, we need to know a few concepts. Let’s first discuss them one by one.

Time.time

  • Time is a public static field in a time class.
  • Time.time returns the time passed in seconds since the game started.

Note:

  • It is affected by Time.timeScale.
  • If the game or application is paused then the Time.time is also be paused and scales or descales according to Time.timeScale value.

Application:

  • Time.time can be used to display the timer in games.
  • It is used to track the total gameplay time excluding the paused time.

Example:

using System.Diagnostics;
using UnityEngine;

public class TimeManager : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        UnityEngine.Debug.Log((int)Time.time);
    }

    // Update is called once per frame
    void Update()
    {
        UnityEngine.Debug.Log((int)Time.time);
    }
}

Output:

if we run the code, then we can see the given result in the console:

Time.time in Unity

Leave a Reply

Your email address will not be published.