x


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.

more ▼

asked Nov 05 '10 at 07:29 PM

Jacob 3 gravatar image

Jacob 3
5 2 2 3

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.

more ▼

answered Nov 05 '10 at 07:49 PM

Peter G gravatar image

Peter G
15.1k 16 44 137

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Nov 06 '10 at 04:10 PM

denewbie gravatar image

denewbie
732 3 3 17

(comments are locked)
10|3000 characters needed characters left

Use Mathf.Floor(value) to get the next smaller integer number. Use Mathf.Ceil(value) to get the next bigger integer number.

more ▼

answered Nov 05 '10 at 07:32 PM

felix. gravatar image

felix.
2k 18 25 47

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3565
x3416
x356

asked: Nov 05 '10 at 07:29 PM

Seen: 1217 times

Last Updated: Nov 05 '10 at 07:42 PM