I’m making a countdown timer with a M:SS layout. Though when I do it, it shows the milliseconds. Here’s what I’ve done:
public Text timerText;
float seconds = 30;
float minutes = 1;
void Update ()
{
seconds -= Time.deltaTime;
if (seconds <= 0)
{
minutes -= 1;
}
}
void OnGUI()
{
timerText.text = minutes + ":" + seconds;
}
}
I would also like the answer in C Sharp form since I find Javascript hard… very hard…
Just round up you second counter in the OnGUI method.
void OnGUI()
{
timerText.text = minutes + ":" + Mathf.Round(seconds);
}
Just change
float seconds
to
int seconds=30;
Take a look at C# string formatting: Custom Numeric Format Strings
Rounding is not actually what you want, for a stopwatch you usually only cut off the smallest increments. String formatting is also very flexible and easy to understand:
public Text timerLabel;
float seconds = 30;
float minutes = 1;
void Update()
{
seconds -= Time.deltaTime;
if (seconds <= 0)
{
minutes -= 1;
}
}
void OnGUI()
{
//timerLabel.text = minutes + ":" + seconds;
timerLabel.text = string.Format("{0:00}:{1:00}", minutes, seconds);
}
You simple put variables into a string like this:
string.Format("First variable: {0}, Second variable: {1}", minutes, seconds);
With “:00” you tell the formatter to only use two characters after the period, if the variable is a number.