I'm trying to get and store max elapsed time in min:sec format. Also, the best time for Highscore. Not working!

After trying out many different approaches, the following two options seems to get the closest to work out:-

The other incomplete approach is commented out in the script


public TextMeshProUGUI timeCounterText;
public TextMeshProUGUI maxTimeText;

/* private float maxTimeSeconds;
private float maxTimeMinutes;
``private float runTimeMin;
private float runTimeSec; */
private float startTime;
private float runTime;
private float maxTime;
private float gameplayTime;

void Start()
{
gameRunTime = true;

    startTime = Time.timeSinceLevelLoad;
 /* maxTimeMinutes = PlayerPrefs.GetFloat("Max Time in Mins", maxTimeMinutes);
    maxTimeSeconds = PlayerPrefs.GetFloat("Secs in max time", maxTimeSeconds); */
    maxTime = PlayerPrefs.GetFloat("Max Time", maxTime);

void Update()
{
if (gameRunTime == true)
{
gameplayTime += Time.smoothDeltaTime * 10;
}

    int seconds = (int)(gameplayTime % 60);
    int minutes = (int)(gameplayTime / 60f) % 60;
    timeCounterText.text = minutes.ToString("00") + ":" + seconds.ToString("00");

  /*  float runTimeSec = (float)seconds; 
    float runTimeMin = (float)minutes;

    UpdateMaxMins();
    UpdateMaxSec();

    maxTimeText.text = maxTimeMinutes.ToString("00") + ":" + maxTimeSeconds.ToString("00"); */
          
    runTime = gameplayTime - startTime;

    if (runTime > maxTime)
    {
        maxTime = runTime;
        PlayerPrefs.SetFloat("Max Time", maxTime);
        
        PlayerPrefs.SetFloat("", runTime);
        
        
    }
    maxTimeText.text = maxTime.ToString("00:00"); 

/*
private void UpdateMaxMins()
{
if (runTimeMin > maxTimeMinutes)
{
maxTimeMinutes = runTimeMin;
PlayerPrefs.SetFloat(“Max Time in Mins”, maxTimeMinutes);
}
}

private void UpdateMaxSec()
{
    if ((runTimeMin > maxTimeMinutes)  )
    {
        if ((gameRunTime == false))
        {
            maxTimeSeconds = runTimeSec;
            PlayerPrefs.SetFloat("Secs in max time", maxTimeSeconds );
            
        }

*/

public void GameOver()
{
gameOverText.text = “GAME OVER”;
gameOver = true;

    if (gameOver)
    {
        gameRunTime = false;
    }

}[108574-new-text-document.txt|108574]

The above script works except for maxTime keeps counting till 100 (instead of 59) and then adds a 1 minute and starts counting seconds again. I need it to count till 59 for seconds and add 1 min and resumes counting seconds and so on, just like timeCounter.
And the commented out script, it simply stops functioning as I store runTimesec to maxTimeSeconds, as in no outputs.

I’m new to unity and C#. Stuck here for a day and half :frowning: I’d really appreciate any suggestions or solutions. Thanks in advance!

If you want a minute counter try this:

  private float time;
     private int minutes;
     void Update(){
     time += time.deltaTime;
     if(time >= 60)
     {
         minutes += 1;
         time = 0;
     }
     yourText.text = minutes.ToString() + ":" + (int)time.ToString();
     }
     
     //if you want to store high scores
     private void HighScore()
     {
         float totalTime = (minutes * 60)+ time;
         if(PlayerPrefs.GetFloat("highestTime") < totalTime){
         PlayerPrefs.SetFloat("highestTime", totalTime);
     }
     
     }

@Sandra-Burgle Thanks a lot for getting back to me on my question, really appreciate it!

I just commented out my current script and ran yours. Unfortunately, it seems yours gets me the same output as my current (see following snippet) script mentioned above, is getting me.

    private float startTime;
    private float runTime;
    private float maxTime; 
    private float gameplayTime;
void Start() 
{
        gameRunTime = true;
        startTime = Time.timeSinceLevelLoad;
        maxTime = PlayerPrefs.GetFloat("Max Time", maxTime);
}
if (gameRunTime == true)
        {
            gameplayTime += Time.smoothDeltaTime * 10;
        }
void Update()
    {
        int seconds = (int)(gameplayTime % 60);
        int minutes = (int)(gameplayTime / 60f) % 60;
        timeCounterText.text = minutes.ToString("00") + ":" + seconds.ToString("00"); 
        
        runTime = gameplayTime - startTime;

        if (runTime > maxTime)
        {
            maxTime = runTime;
            PlayerPrefs.SetFloat("Max Time", maxTime);   
        }
        maxTimeText.text = maxTime.ToString("00:00");
    }

But, yours is definitely done much simpler and lesser codes and enlightened me with new possible options which I’ll try explore and see if it gets me where I aiming to.
Thanks again!! :slight_smile: