0
\$\begingroup\$

So I have this infinite runner type of game, subway surfers style. I chose a method where the player doesn't move, and instead the obstacles move towards him. My score is a float and it increases as time passes. However, I'm unable to figure out how can you save this as a highscore. I tried a few methods but most of them were for int so it didn't work out. Here is what I have for the score counting. It's currently missing the save score part completely.

    public class GameManagerC : MonoBehaviour
{
    float score = 0;
    public float scoreMulti = 5;
   
    void Update()
    {
        if (isPlayerAlive == true)
        {
            score += Time.deltaTime * scoreMulti;
            scoreText.text = score.ToString("Score: 0");
        }
    }

EDIT: Many of you asked how I tried to implement the code I've found so here it is:

public float score;
public float highScore;



void Start()
    {
        score = PlayerPrefs.GetFloat("score");
        highScore = PlayerPrefs.GetFloat("highScore");
        if (score > highScore)
            highScore = score;
        StartGame();
    }

    // Update is called once per frame
    void Update()
    {
        if (isPlayerAlive == true)
        {
            score += Time.deltaTime * scoreMulti;
            scoreText.text = score.ToString("Score: 0");
        }
    }
\$\endgroup\$
10
  • \$\begingroup\$ What did you try, specifically, and how did it go wrong? \$\endgroup\$
    – DMGregory
    Feb 4, 2021 at 19:25
  • \$\begingroup\$ Hi, 'Can I save a score ... if it's a float', I don't see why not? I wonder if you really need a float, but if you want an int (for example), you can just round it up (be generous to the player :P) to a whole number, after you calculate it with deltatime and such right? And while the game is running, you can calculate your score as float and show a rounded value to the player (don't store it as int or you may round-on/off a small amount each time). \$\endgroup\$
    – D.Kallan
    Feb 4, 2021 at 19:44
  • \$\begingroup\$ Can we see the high score script that didn’t work? We can help fix it if we can see it. \$\endgroup\$ Feb 4, 2021 at 19:51
  • \$\begingroup\$ Thanks for the fast reach out. I tried to modify this: forum.unity.com/threads/… Pretty much copied it and changed the runtime and best time into score and highscore \$\endgroup\$ Feb 4, 2021 at 20:08
  • \$\begingroup\$ Include your code what you did and what it is doing wrong. \$\endgroup\$
    – Zibelas
    Feb 4, 2021 at 21:57

2 Answers 2

1
\$\begingroup\$

First, you will want to add public float highscore; with your other variables at the top. Then, you will want to change the old code to this in your script. This is a very basic save system, and can easily be modified to fit the needs of the game. The comments in the code explain the script in further detail.

void Update()
{
    // Increases score if player is still alive
    if (isPlayerAlive == true)
    {
        score += Time.deltaTime * scoreMulti;
        scoreText.text = score.ToString("Score: 0");

        // Changes highscore to current score if current score is higher
        if (score > highscore)
        {
            highscore = score;
        }
    }
}

// Saves the score when activated, from button, script, etc.
void SaveScore()
{
    PlayerPrefs.SetFloat("score", score);
    PlayerPrefs.SetFloat("highscore", highscore);
    PlayerPrefs.Save();
}

// loads the score and highscore from the save file, if there are any
void LoadScore()
{
    score = PlayerPrefs.GetFloat("score", 0);
    highscore = PlayerPrefs.GetFloat("highscore", 0);
}

  
\$\endgroup\$
0
1
\$\begingroup\$

So I could succesfully implement a different method from this guy: https://answers.unity.com/questions/1378780/save-timer-highscore-1.html Thanks everyone for your patience and help! Currently this is able to save my highscore if I hit the restart button to reload my scene. If you stop playing and hit the play again however it resets to zero, so there's a chance this isn't what others are looking for.

Modified code:

public TextMeshProUGUI scoreText;
public TextMeshProUGUI highScore;
float score;



// Start is called before the first frame update
    void Start()
    {
        StartGame();
        highScore.text = PlayerPrefs.GetFloat("HighScore", 0).ToString();
    }
    // Update is called once per frame
    void Update()
    {
        if (isPlayerAlive == true)
        {
            score += Time.deltaTime * scoreMulti;
            scoreText.text = score.ToString("Score: 0");

            if (score > PlayerPrefs.GetFloat("HighScore", 0))
            {
                PlayerPrefs.SetFloat("HighScore", score);
                highScore.text = score.ToString();
            }
        }
    }
\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .