2
\$\begingroup\$

I'd like to save the player's high score using Unity's PlayerPrefs API. This is the code I have so far:

public int coins;
public int score;
public int highscore;
public int points;
public Text pointsText;
public Text HighScore;
public Text Score;
public Text coinsText;

void start(){
    highscore = PlayerPrefs.GetInt ("HighScore");
}
void Update () {
    if(points > highscore){
        highscore = points;
        HighScore.text = ("HighScore: " + points);
        PlayerPrefs.SetInt ("HighScore", highscore);
        PlayerPrefs.Save ();
    }
    score = points;
    pointsText.text = ("x  " + points); 



    Score.text = ("Score  " + points);
    PlayerPrefs.SetInt ("Score", points);

    coinsText.text = ("Coins  " + points);

}

However, it does not work. Every time I restart the game, the high score is 0 again. How can I make this work?

\$\endgroup\$
0

1 Answer 1

3
\$\begingroup\$

The problem appears to be, as I noted in the comments originally, that you have a function called start() in your code (which I am presuming is a MonoBehavior class). C# is case-sensitive, however, and the MonoBehavior function that is invoked at the start is called Start() (with an upper-case "S").

Your start() function is likely not being called at all, which means the code to read the high score from the PlayerPrefs is never executed, at the high score remains at its default value of 0.

\$\endgroup\$
2
  • \$\begingroup\$ can you change that script to work? pls? \$\endgroup\$ Commented Jun 2, 2017 at 10:17
  • \$\begingroup\$ @RicardoCarapeto Capitalize "start." Change the "s" to "S" -- that is all I am suggesting. \$\endgroup\$
    – user1430
    Commented Jun 2, 2017 at 13:28

You must log in to answer this question.

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