- Home /
Removing the tenths of seconds on a timer
//Variable for in-game timer var myTimer : float = 30;
//Script for the in-game timer function Update () { if(myTimer > 0){ myTimer -= Time.deltaTime; }
if(myTimer <= 0){
Application.LoadLevel (2);
}
}
//Script to implament the in-game timer function OnGUI(){ GUI.Label(Rect(10,10,200,50), "Time: " + myTimer); }
I have set up a timer for my game, however I would like to remove the numbers after the decimal and display the time in whole seconds.
I just don't know how I would do that, any advice would be great.
Answer by Peter G · Nov 05, 2010 at 07:49 PM
Here's the most optimized way. See my answer.
http://answers.unity3d.com/questions/25979/timer-display-question/25984#25984
Use InvokeRepeating() and you will save method calls meaning faster performance for the same result.
Answer by denewbie · Nov 06, 2010 at 04:10 PM
just do this:
GUI.Label(Rect(10,10,200,50), "Time: " + myTimer);
change to
GUI.Label(Rect(10,10,200,50), "Time: " + (int)myTimer);
by parsing the value into an integer the timer automatically returns you whole numbers.
Answer by fherbst · Nov 05, 2010 at 07:32 PM
Use Mathf.Floor(value) to get the next smaller integer number. Use Mathf.Ceil(value) to get the next bigger integer number.
Your answer
![](https://koobas.hobune.stream/wayback/20220613082059im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Deactivating a power up script after a certain amount of time 1 Answer
Sprinting script, timer? 0 Answers
Start timer with Input 2 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
How do I make a sword swing? 2 Answers