- Home /
Clock in Unity
How do you make a clock in Unity?
$$anonymous$$any ways... Do you want a timer counting up, a timer counting down, an analog clock, a digital clock?
A reasonably simple timer counting up setup would be:
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class SimpleClock : $$anonymous$$onoBehaviour {
// set this in the editor
public Text clockDisplay;
int secondsPassed = 0;
// Use this for initialization
IEnumerator Start () {
while (true)
{
clockDisplay.text = secondsPassed.ToString();
yield return new WaitForSeconds(1);
secondsPassed++;
}
}
}
Answer by kalanadis · Jul 23, 2017 at 06:09 AM
You can use system time http://answers.unity3d.com/questions/24187/how-to-get-system-time.html
Or You can use a web service
Or if its a game world thing where you can initialize within the game here's few tips
FixedUpdate()
is called in a fixed timestep (unlike Update()
)
you can set the fixed time step from Edit ==> Project Settings ==> Time Fixed Timestep value. (value is in seconds)
https://docs.unity3d.com/Manual/class-TimeManager.html
So you can use a int
variable inside a FixedUpdate()
and increment it in each update. and to get the time you can multiply with time step
Your answer
Follow this Question
Related Questions
Weird Problem with Clock 3 Answers
Performance question: updating a clock 1 Answer
Adjusting DateTime values 1 Answer
incrementing instead of speeding up ingame clock 2 Answers
clock script 0 Answers