Duplicate. Do some research before asking a new question.
http://answers.unity3d.com/questions/348742/time-formatting.html
How do you hide milliseconds when making a countdown timer?
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..
What have you tried?
Check out Xarbrough's answer.
You can google most of your questions. This avoids duplicate questions.
Answer by Kurtisi · Apr 17, 2016 at 02:48 PM
Just round up you second counter in the OnGUI method.
void OnGUI()
{
timerText.text = minutes + ":" + Mathf.Round(seconds);
}
Answer by Xarbrough · Apr 17, 2016 at 03:16 PM
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.
Follow this Question
Related Questions
Unity3d countdown timer is not counting down 0 Answers
Countdown timer for starting game 0 Answers
Need to create a Time Trial in Unity with C# 1 Answer