x


Timer Display Question

static var timeRemaining : float = 60;
var prefix = "Time Left: ";
var suffix = " ";

function Awake(){
timeRemaining = 60;

}

function Update()
{
guiText.text = prefix + timeRemaining + suffix;

   timeRemaining -= Time.deltaTime;

if(timeRemaining <= 0)
{
Application.LoadLevel ("Game Over");
}
transform.position.x = 0.65;
transform.position.y = 0.05;
}

I have a timer but it displays the numbers after the decimals while I only want it to display whole numbers. Any suggestions?

more ▼

asked Nov 02 '10 at 08:00 PM

Persona gravatar image

Persona
246 74 85 96

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

5 answers: sort voted first

If you just want the ceiling:

guiText.text = prefix + Mathf.Ceil(timeRemaining) + suffix;

If you just want to drop the decimal and get the floor:

guiText.text = prefix + Mathf.Floor(timeRemaining) + suffix;
more ▼

answered Nov 02 '10 at 08:08 PM

skovacs1 gravatar image

skovacs1
10k 11 25 91

Simple and effective. I'll take it!

Nov 02 '10 at 08:54 PM Persona
(comments are locked)
10|3000 characters needed characters left

If you want to truncate the time, you could have Unity do the timing for you and then just print that.

var time: int = 60;

function Awake () {
    //Create the timer.
    //Method: InvokeRepeating("MethodName", delayFor1stFire : float, repeatTime: float);
    InvokeRepeating("FireTimer", 1, 1);

    //I could not figure out why you were calling these every frame.
    transform.position.x = 0.65;
    transform.position.y = 0.05;
}

function FireTimer() {
    time--;

    guiText.text = prefix + time.ToString() + suffix;

    if(timeRemaining <= 0)
    {
         Application.LoadLevel ("Game Over");
    }
}

This will improve performance as well because you are not calling the method every frame. Only once per second. So, if performance matters, this is the fastest method.

more ▼

answered Nov 02 '10 at 08:22 PM

Peter G gravatar image

Peter G
15k 16 44 136

Oooh, very nice :)

Nov 02 '10 at 08:45 PM Atnas1010
(comments are locked)
10|3000 characters needed characters left
guiText.text = prefix + timeRemaining.ToString("f0") + suffix;

This should work.

more ▼

answered Nov 02 '10 at 08:02 PM

Atnas1010 gravatar image

Atnas1010
1.1k 6 10 26

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

You could try Mathf.Round() or something similar, or just convert the float to an int it will drop the fraction.

var intTimeRemaining : int = timeRemaining;
guiText.text = prefix + intTimeRemaining + suffix;

Unity JS has dynamic typecasting, so it should just work. I didn't test it, though.

Cheers

==

more ▼

answered Nov 02 '10 at 08:03 PM

equalsequals gravatar image

equalsequals
4.5k 16 28 64

Converting the float to an int would make the counting stop, since Time.deltaTime is a float

Nov 02 '10 at 08:06 PM Atnas1010

I believe equalsequals was referring to converting to an int inside the guiText.text, not at your declaration - this would not stop the counting.

Nov 02 '10 at 08:10 PM skovacs1

How would one do that?

Nov 02 '10 at 08:13 PM Atnas1010

edited my post.

Nov 02 '10 at 08:23 PM equalsequals
(comments are locked)
10|3000 characters needed characters left

You can use an integer and subtract one every second, instead of using a float in Update. See my answer here: http://answers.unity3d.com/questions/12992/count-down-timer-per-second

more ▼

answered Nov 02 '10 at 10:10 PM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

I believe that is what I did. ;)

Nov 02 '10 at 10:27 PM Peter G

@Peter G: Well, so it is...maybe I should read the other answers more thoroughly next time.... Pity the accepted answers are the less efficient ones. ;)

Nov 02 '10 at 10:46 PM Eric5h5

Hey, I go for simple enough for me to understand. Besides, I openly admit, I'm too incompetent right now to know the difference in the CPU cycle.

Nov 03 '10 at 02:12 AM Persona
(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:

x3698
x352
x146

asked: Nov 02 '10 at 08:00 PM

Seen: 2048 times

Last Updated: Nov 02 '10 at 08:00 PM