- Home /
Timer's tenths of seconds visible digits issue
I use a simple script for displaying the tenths of seconds on a timer. The following script is called GUITimerFragment.js and is applied to an empty game object I call GUI.
var startTime;
function Awake () {
startTime = Time.time;
}
function OnGUI () {
var runningTime = ((Time.time-startTime)*10)%10;
runningTime = String.Format ("{0:0}", runningTime);
GUI.Label(Rect(83,323,200,200), runningTime.ToString());
}
The issue is this: the timer begins counting the tenths of seconds normally: 1...2...3...4...5...6...7...8...9...10 only that I want it to display 0 instead of 10. I thought that the line
String.Format ("{0:0}", runningTime)
would make it show only one digit but it seems I'm wrong.
What I see is this (vimeo video)
What do I have to do to make it show "0" instead of "10"? - thanks :-)
you could do if(runningTime == 10) {runningTime = 0} to reset it to 0 everytime it 'should' be 10.
Answer by Statement · Dec 09, 2010 at 09:52 PM
It seems there was some strange thing going on where you were using the mod operator on a float. I cast this to an integer to floor the value.
Also String.Format ("{0:0}", runningTime); means to return runningTime without any decimals. It doesn't constrain the number to one digit.
var startTime : float;
function Awake () { startTime = Time.time; }
function OnGUI () { var runningTime : int = ((Time.time - startTime) * 10) % 10; var runningTimeText : String = String.Format ("{0:0}", runningTime); GUI.Label(Rect(83,323,200,200), runningTimeText); }
Here is another way of achieving the same result:
import System;
var startTime : float; var rect = Rect ( 83, 323, 200, 200 );
function Awake () { startTime = Time.time; }
function OnGUI () { var span = TimeSpan.FromSeconds(Time.time - startTime); var tenths = span.Milliseconds / 100;
GUI.Label ( rect, tenths.ToString() );
}
Thanks for your answer. The float was indeed a problem. You helped me make things more clear. In the second script though, what is the role of "import System"? Excuse me if this is a total noob query, but I don't know where to look it up.
import System; reduces the namespace System to global namespace. It is there because Time and TimeSpan are types that reside in the "System" namespace. Look up "namespace" in wikipedia. Without import System; we have to type in the full name to the types, such as: System.Time and System.TimeSpan. It is the same as using System; in C#.
Answer by yoyo · Dec 09, 2010 at 11:36 PM
For formatting elapsed time for GUI display I use this method ... easily tweakable if you want tenths, or hours or other variations.
public string FormatTime(float time)
{
int d = (int)(time * 100.0f);
int minutes = d / (60 * 100);
int seconds = (d % (60 * 100)) / 100;
int hundredths = d % 100;
return String.Format("{0:00}:{1:00}.{2:00}", minutes, seconds, hundredths);
}
Your answer
Follow this Question
Related Questions
Referencing other scripts: -5 seconds rule on timer 1 Answer
How to make a timer? 1 Answer
My high time score isn't working 1 Answer
CountDown Timer Help (Seconds problem) 2 Answers
why the debree timer float not work 1 Answer