converting timer countdown to only show minute and seconds
So I'm using C# and have a count down timer, if the timer hits 0 then the player dies. i have a timer working but its showing seconds and milliseconds only (e.g. 6.5377) i need to change it to only show minute and seconds. the time shows up on my GUI just fine (other than showing seconds/milliseconds) This is the code for the timer
public float timeRemaining = 120;
void Update ()
{
if (timeRemaining <=0)
{
timeRemaining =0;
}
timeRemaining-=Time.deltaTime;
}
and this is the code for the GUI to show
public Text timeRemaining;
public GameObject countDownRef;
void Update()
{
timeRemaining.text= countDownRef.GetComponent().timeRemaining.ToString();
}
please format your code. Select it all and press the '1010101' button or ctrl+$$anonymous$$
sorry didnt know how to format it, thanks Scribe for letting me know
Answer by Bob-The-Zealot · Aug 31, 2015 at 12:45 PM
What your looking for is the math operation mod.
The mod symbol is %
.
You would do: seconds = totalseconds % 60;
to calculate the seconds,
and minutes = Mathf.Round(totalseconds / 60)
to calculate the minutes;
So you would do this:
timeremaining.text = Mathf.Round(totalseconds/60) + ":" + totalseconds % 60;
how do I implement this into my code? I'm still rather new to coding
@Bob the Zealot i got it showing the $$anonymous$$ute and second, but it still shows milliseconds also the time isnt count correctly, e.g. if totalSeconds is set to 180 it starts at 4:00 but then when time gets down to 3:30 it then becomes 2:29 when it hits 2:00 then it becomes 2:59 then at 2:30 it becomes 1:29 its not until the last 30seconds that the timer has the correct time is this how you meant for it to be set up on the canvas script
public Text timeRemaining; public float totalSeconds = 100; timeRemaining.text = $$anonymous$$athf.Round(totalSeconds/60) + ":" + totalSeconds % 60; totalSeconds-=Time.deltaTime;
Your answer
![](https://koobas.hobune.stream/wayback/20220612002049im_/https://answers.unity.com/themes/thub/images/avi.jpg)