- Home /
Count Down Timer.... (per second)
How would I add a count down timer that counts down per second and displays how many seconds left?
My Current Code: (yes I know that the Update function is incorrect but I'm just testing the decrementing)
var seconds = 60;
function Update () { seconds --; var a = GameObject.Find ("Timer"); var testMesh : TestMesh = a.GetComponent(TextMesh); testMesh.text = seconds.ToString ();
When I used this code.... it went way too fast and went to negatives.
So can someone tell me a professional script (stops at zero and changes per SECOND)? Thanks!
Answer by Mike 3 · Jun 21, 2010 at 08:31 AM
Something like this should work:
var endTime : float; var textMesh : TextMesh;
function Start() { endTime = Time.time + 60; textMesh = GameObject.Find ("Timer").GetComponent(TextMesh); textMesh.text = "60"; }
function Update() { var timeLeft : int = endTime - Time.time; if (timeLeft < 0) timeLeft = 0; textMesh.text = timeLeft.ToString(); }
Perfect $$anonymous$$ike. Thanks for answering like 5 of my questions perfectly!
$$anonymous$$ight you answer one more? Thanks. The other guy didn't understand what I meant.
Answer by Eric5h5 · Jun 21, 2010 at 03:07 PM
Here's another method...it only fires once per second, and stops when done, so it doesn't use any unnecessary CPU cycles:
var seconds = 60; private var textMesh : TextMesh;
function Start () { textMesh = GameObject.Find ("Timer").GetComponent(TextMesh); textMesh.text = seconds.ToString(); InvokeRepeating ("Countdown", 1.0, 1.0); }
function Countdown () { if (--seconds == 0) CancelInvoke ("Countdown"); textMesh.text = seconds.ToString(); }
Can you also answer this question? Thanks! http://answers.unity3d.com/questions/13016/stay-in-trigger-range-script
@$$anonymous$$ikezNesh: it only runs once per second ins$$anonymous$$d of every frame, and stops when it's done ins$$anonymous$$d of continuing to run every frame (and uses one less variable), so indeed it's more efficient.
Answer by heruka · Jul 12, 2015 at 12:00 PM
Here is my adapted solution using Unity 5 and following the video at https://www.youtube.com/watch?v=SbehR3Ad3oU
My solution is in my "GameController" script, so the formatting (position, font, type, etc) are set in the inspector window. Also, the "GameOver" function I call, is inside the "GameController" script. Make sure you've got Using UnityEngine.UI listed at the top. I've also place the "(int)" in front of time remaining because I wanted it to countdown per second vs the timer showing per frame. Hope this helps!
public Text cdtimerText;
public float timeRemaining;
// Use this for initialization
void Start () {
cdtimerText.text = "";
}
void Update ()
{
// Functional game countdown timer
timeRemaining -= Time.deltaTime;
if (timeRemaining > 0) {
cdtimerText.text = "TIME: " + (int)timeRemaining;
} else {
cdtimerText.text = "TIME'S UP!";
GameOver ();
}
}
Your answer
Follow this Question
Related Questions
Timer doesn't work properly 2 Answers
When Score goes up by 25 change timer value 1 Answer
Countdown timer trouble? 1 Answer
Countdown to start level 1 Answer
Count down on gameobject 1 Answer