- Home /
Timer and gui text
hi guys this is my simple timer and is working perfect, but how i can display the countdown of the timer in screen with a gui text i get a error when i try and i must do something with string...
var timer : float = 10;
function Update(){
timer -= Time.deltaTime;
}
so we have the variable timer and is float and equal with 10 and timer get subtracted with Time.deltaTime every frame so that it but how i can display the minutes/seconds as gui text?
Answer by aldonaletto · Jul 27, 2012 at 02:39 AM
You must create a GUIText and set a reference to it in your script, which will become something like this:
var gText: GUIText; // drag here the GUIText from Hierarchy view
var timer: float = 10;
function Update(){
timer -= Time.deltaTime;
if (timer < 0) timer = 0; // clamp the timer to zero
var seconds: int = timer % 60; // calculate the seconds
var minutes: int = timer / 60; // calculate the minutes
gText.text = minutes + ":" + seconds;
}
This formatting doesn't work fine with negative numbers, thus the timer is clamped to zero. If you need to display negative values, change the function Update to this:
...
function Update(){
timer -= Time.deltaTime;
var t = Mathf.Abs(timer); // get the absolute timer value
var seconds: int = t % 60; // calculate the seconds
var minutes: int = t / 60; // calculate the minutes
var minSec = minutes + ":" + seconds; // create the formatted string
if (timer < 0) minSec = "-" + minSec; // add a minus sign if negative
gText.text = minSec; // update the GUIText
}
EDITED:
String concatenation isn't a good idea because it generates lots of intermediate memory allocations, which may cause performance hiccups when garbage collection (free memory recuperation) takes place. A better solution is to use .NET/Mono String.Format:
gText.text = String.Format("{0:00}:{1:00}", minutes, seconds);
Each number is defined in the formatting string inside curly brackets, where the digit before the colon tells which argument to use (starting at 0), and the digits after the colon show the format. Take a look at .NET docs for more details.
i find very nice tips this and i understand the code 100% nice job :D
@D$$anonymous$$ Reigns, you can separate the fractional part with % 1 (modulo 1):
var mSecs: float = t % 1;
and format the number with 3 decimal places:
gText.text = mSecs.ToString("F3");
But if you want the milliseconds as an integer, multiply the result by 1000 and format it with no decimal places:
var mSecs: float = (t % 1)*1000;
gText.text = mSecs.ToString("F0");
Answer by AlucardJay · Jul 27, 2012 at 01:54 AM
This topic has been discussed extensively. A search engine is your friend ...
http://lmgtfy.com/?q=unity+gui+timer
http://answers.unity3d.com/questions/11458/How-to-make-a-timer-in-the-game.html
http://answers.unity3d.com/questions/45455/how-to-start-a-timer-display-as-gui.html
http://www.youtube.com/watch?v=aHf96fBBYM0&feature=player_embedded
Your answer
Follow this Question
Related Questions
Timer progress bar 0 Answers
Countdown Timer 1 Answer
Problem with Footstep System 1 Answer
GUI elements vanish when publishing 1 Answer
How Would I Make A GUI Label Fade After A Certain Amount Of Time? 1 Answer