Home > other >  How do I update my in-game timer to only start adding the minute counter once it reaches 60 seconds
How do I update my in-game timer to only start adding the minute counter once it reaches 60 seconds

Time:11-01

I only want the numbers to change to minutes:seconds once a minute has been reached. Before that I just want the seconds. My timer is currently set up as follows:

   time  = Time.deltaTime;
   string minutes = Mathf.Floor(time / 60).ToString("00");
   string seconds = Mathf.RoundToInt(time % 60).ToString("00");

    gameTime.text = string.Format("{0}:{1}", minutes, seconds);

CodePudding user response:

Fix your code like this:

        time  = Time.deltaTime;
        string minutes = Mathf.Floor(time / 60).ToString("00");
        string seconds = Mathf.RoundToInt(time % 60).ToString("00");
        if (time >= 60){
            gameTime.text = string.Format("{0}:{1}", minutes, seconds);
        }
        else
        {
            gameTime.text = seconds;
        }

CodePudding user response:

Well without magic but simple straight forward you could do

var minutes = Mathf.FloorToInt(time / 60f);
var seconds = Mathf.RoundToInt(time % 60f);

if(minutes > 0)
{
    gameTime.text = $"{minutes:00}:{seconds:00}";
}
else
{
    gameTime.text = $"{seconds:00}";
}
  • Related