-1
\$\begingroup\$

So I want to make hill climb wanna be game but have an issue with saving bestTime each level, the point is I want to save the best time for each level to playerprefs also save it on text value in main menu like the image.

here my timer script :

 public void TimerSystemOn()
{
    if (timerIsRunning)
    {
        if (timeRemaining > 0)
        {
            timeRemaining -= Time.deltaTime;
            DisplayTime(timeRemaining);
        }
        else
        {
            if (timeRemaining <= 0)
            {
                Debug.Log("Time has run out!");
                timeRemaining = 0;
                timerIsRunning = false;

                if (!timerIsRunning)
                {
                    TimeOut();
                }
            }
        }
    }

    if (!timerIsRunning)
    {
        ReachFinishLine();
        DisplayTime(timeRemaining);

    }
}

void DisplayTime(float timeToDisplay)
{
    timeToDisplay += 1;

    float minutes = Mathf.FloorToInt(timeToDisplay / 60);
    float seconds = Mathf.FloorToInt(timeToDisplay % 60);

    PlayerPrefs.SetFloat("Minutes", minutes);
    PlayerPrefs.SetFloat("Seconds", seconds);
    timeText.text = string.Format("{0:00}:{1:00}", minutes, seconds);

    float highscoreMins = PlayerPrefs.GetFloat("Minutes");
    float highscoreSec = PlayerPrefs.GetFloat("Seconds");

    if (minutes > highscoreMins || seconds > highscoreSec)
    {
        PlayerPrefs.SetFloat("Minutes", minutes);
        PlayerPrefs.SetFloat("Seconds", seconds);
    }
    Debug.Log("HighscoreSec : " + highscoreSec);
}

that's my current script to save the bestTime. and this script call on the game, when died the timer value or time remaining = 0 and when the car reached the destiny timer will be stopped but the value saved to best time I confused here about saving that value and save it on each level

I want to save it in the bottom text. enter image description here

\$\endgroup\$
  • \$\begingroup\$ No offence, but it's unclear what you need help with. See stackoverflow.com/help/how-to-ask \$\endgroup\$ – Jim U Nov 16 at 12:07
  • \$\begingroup\$ I've edited that @JimU \$\endgroup\$ – YouCanCallMe Syarif Nov 16 at 14:33
  • \$\begingroup\$ It's still not clear what your problem is. Does this script not do what you expect it to do? What does it do instead? \$\endgroup\$ – Philipp Nov 16 at 14:44
  • \$\begingroup\$ @Philipp is that clear? \$\endgroup\$ – YouCanCallMe Syarif Nov 16 at 14:57
  • \$\begingroup\$ @YouCanCallMeSyarif There doesn't seem to be any code in your question which handles the case that the car reached the destination. You might want to do that first before you think about how to save the score in that case. \$\endgroup\$ – Philipp Nov 16 at 15:00
0
\$\begingroup\$

You are basically always saving the time as highscore. The comments are explaining why your current code is not doing what it should.

//Here you save already to your playerPref
PlayerPrefs.SetFloat("Minutes", minutes);
PlayerPrefs.SetFloat("Seconds", seconds);
timeText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
//Here you load the same time that you just saved
float highscoreMins = PlayerPrefs.GetFloat("Minutes");
float highscoreSec = PlayerPrefs.GetFloat("Seconds");
//This condition does nothing since the time is the same value and we saved it already
if (minutes > highscoreMins || seconds > highscoreSec)
{
    PlayerPrefs.SetFloat("Minutes", minutes);
    PlayerPrefs.SetFloat("Seconds", seconds);
}

If you need a highscore per level, you might add a level prefix. And you can always save the time in total seconds since you even have the time already in seconds. For display purpose you convert it, for saving it is not needed. Keep it simple with one value.

You should as well not mix saving and displaying. Make an extra saving function that can determinate handles saving your highscore.

(Your display method should as well not handle the increase of time, this is another part you should rework)

void DisplayTime(float timeToDisplay){
     timeToDisplay += 1;
     float minutes = Mathf.FloorToInt(timeToDisplay / 60);
     float seconds = Mathf.FloorToInt(timeToDisplay % 60);   
     timeText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
}

void SaveIfHighScore(float time, string level){
     float currentHighscore = PlayerPrefs.GetFloat("Time_" + level);
     if (time > currentHighscore) {
         PlayerPrefs.SetFloat("Time_" + level, time)
     }
} 

This assumes that highscore means the longer time, the better it is. If you need it to be fastest time, you need to switch the >.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ where should i put Playerprefs.setFloat("Time_") is it on the start ? \$\endgroup\$ – YouCanCallMe Syarif Nov 17 at 10:33
  • \$\begingroup\$ You mean for the initial setting of the time for the very first time? Check if the key exists with HasKey Playerprefs.HasKey("Time_" + level), and if that is false, you can init the default highscore for certain levels \$\endgroup\$ – Zibelas Nov 17 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.