Saving time with playerprefs

Hey!

In PlayerPrefs, you can call 3 things:

-SetInt
-SetFloat
-SetString

I have a game with a timer, and I want the fastest time to be shown in the main menu.

How to do that?

When the player reaches the end of the level, save the time as a float. If there is no best time yet, save with PlayerPrefs.SetFloat(“BestTime”, theFloatVariable) . If there is already a time, check if the new time is < and only then save it. Then in menu, show PlayerPrefs.GetFloat

1 Like

Okay, thanks for your reply

But how do I display my time like this?

Highscore is 08:24 (for example)

Thanks in advance

https://docs.unity3d.com/ScriptReference/UI.Text.html
Place a text field on your canvas. As you are working with playerprefs you don’t need to reference it. Can just put a simple script on the text object that uses the player prefs float for the text

BTW I think in this video it’s pretty much like you want:

1 Like

Thanks for that video! I learned a lot about PlayerPrefs. Thanks to you, because you shared the video, I now got my Highscore working, it now only gets updated when your time is faster than the highscore time.
Thanks so much for your time and help mate!

But now there is only one question for me left, which I asked before…

Right now, my highscore is displayed like this: (Yeah I now the game name is terrible, but I have to find another name so I just wrote down ballgame for now :P)

What I really want to be shown in the main menu is this: (Example)

Is that possible? So yes, do you know how?

Thanks in advance again! :slight_smile:

Btw this is the script for highscore:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SaveHighscore : MonoBehaviour
{
    public Text highscoreT;

    void Start()
    {
        highscoreT = GetComponent<Text>();
        highscoreT.text = PlayerPrefs.GetFloat("Highscore", 0).ToString();
    }

    public static void UpdateScore()
    {
        if(Timer.time < PlayerPrefs.GetFloat("Highscore", Timer.time))
        {
            PlayerPrefs.SetFloat("Highscore", Timer.time);
        }
    }
}

And this one is for the timer in my game:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Timer : MonoBehaviour
{
    public static float time;
    public static Text timer_T;

    void Start()
    {
        timer_T = GetComponent<Text>();
    }


    void Update()
    {
        time += 1 * Time.deltaTime;

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

        timer_T.text = "Time:" + " " + minutes + ":" + seconds;
    }
}

I think you already have the solution in your Timer script. Just need to do the same thing like this:
timer_T.text = “Time:” + " " + minutes + “:” + seconds;
Just for your menu highscore text

Allright I understand what your saying, but the problem is, that I can’t convert from float to string inside of playerprefs…

You can convert with .ToString()
Btw if you don’t want to use the decimal digits you can use int instead of float.

Yea allright, but I just don’t know where and in which way to do that in my code.

It would be really appreciated if you could just copy my script and add the things needed to change my highscore from float to minutes:seconds.

If you arent doing it, no problem, I can understand that, but you would really help me out!

You need to make a variable and assign the value you get from the player prefs to it. Then you can do whatever you want with it, like assigning it to the text of a Text component etc.

private Text highScoreText;

void ShowHighscore()
{
      float highscore = PlayerPrefs.GetFloat("HighScore");
     highScoreText.text = highscore.ToString();
}
3 Likes