Answers for "clock in unity"

C#
0

clock in unity

// I'm going to assume that you 
// are looking to make a clock that tick every second

// 1. If your script is MonoBehavior :

class Script : MonoBehavior {
  private float elapsed = 0;
  
  public void Update()
  {
    elapsed += Time.deltaTime; // Time.deltaTime return the number of seconds elapsed from last frame, usually ~1/60s
    if (elapsed > 1) {
      elapsed = 0;
      Debug.Log("Tick !");
    }
  }
}

// 2. If your script is not MonoBehavior

class Script2 {
  public void init()
  {
    Invoke("Tick", 1); // Invoke can call a function after a certain amount of time
  }
  
  public void Tick()
  {
    Debug.Log("Tick !");
    Invoke("Tick", 1);
  }
}
Posted by: Guest on March-11-2021

C# Answers by Framework

Browse Popular Code Answers by Language