x


GUI Text Values keep resetting

I am trying to display through GUI text the amount of calories burned riding a bicycle relative to the amount of time spent riding the bike. The code I have works except for the fact that it resets back to zero every 60 seconds. I can't for the life of me figure out what's wrong. Here's the code that I am using below.

//Format the time nicely

guiText.text = FormatTime (); guiText.material.color=Color.white;

}

//Format time like this

//12(minutes):34(seconds).5(fraction)

function FormatTime () {

var intTime = Time.time;

var seconds = intTime % 60;

switch (vehicle) { case "car" : calories = seconds * .03317; break; case "bike": calories = seconds * .1325; break; case "walk": calories = seconds * .058; break; default: //don't do anything! break; }

calories = Mathf.Floor(calories * 100) / 100;

//print (calories);

            //if seconds are under ten, add a zero



            if (calories < 10) {
                            caloriesText = "0" + calories.ToString();

            } else {

                            caloriesText = calories.ToString();

            }

return caloriesText;

}

Any help would be appreciated!

more ▼

asked Feb 25 '10 at 09:22 PM

user-335 (google) gravatar image

user-335 (google)
51 4 5 8

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

1 answer: sort voted first

You're telling it to reset to 0 every 60 seconds when you use the modulus operator: "var seconds = intTime % 60;". However, most of that actually isn't necessary; you're better off just using string formatting:

function FormatTime () {
    switch (vehicle) {
        case "car" : calories = Time.time * .03317; break;
        case "bike": calories = Time.time * .1325; break;
        case "walk": calories = Time.time * .058; break;
        default: //don't do anything!
        break;
    }
    return calories.ToString("00.00");
}
more ▼

answered Feb 26 '10 at 12:05 AM

Eric5h5 gravatar image

Eric5h5
80.1k 41 132 519

(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:

x5054
x550

asked: Feb 25 '10 at 09:22 PM

Seen: 1111 times

Last Updated: Feb 25 '10 at 09:22 PM