Unity | SoloLearn: Learn to code for FREE!



How can I make a c# script for a timer in unity that displays it in a text box

7/12/2018 8:54:43 PM

Oli H

5 Answers

New Answer


1. Make a text element in the editor 2. Reference the text element by writing using UnityEngine.UI; public Text textBox; 3. Make a timer public int coundownAmount; private int startTime; private void Update() { if (startTime == 0) { startTime = Time.time; } textBox.text = (countdownAmount - (Time.time - startTime)).ToString(); } This is assuming you know how to create a script and add it somewhere in the scene. I'm also assuming you actually want a timer (one that counts down) and not a stopwatch (one that goes up) Of course, there are multiple ways to do it. Feel free to ask further questions!




it was a stopwatch tho and do you need to create a text box in the heirachy in unity


For a stopwatch: Step 1&2 like before 3. Make a stopwatch private float uptime; private void Update() { uptime += Time.deltaTime; textBox.text = uptime.ToString(); }