How do i save Highscore?

Heres the code im using for the score (note its not the full code just the code i used for score)

//using for score
public static int score = 0;

//drawing it on screen
void OnGUI ()
    {
        GUI.color = Color.black;
        GUILayout.Label(" Score: " + score.ToString());
    }

thanks

You should really get away from using OnGui. It’s best used for Editor scripts now. The newer gui is preferred and better than OnGui.

As far as saving score. You just have to save the value of the score variable. But as you really didn’t tell us much, my only guess is you want to save it to a PlayerPref and not online or to a backend service, etc.

PlayerPrefs.SetInt("HighScore", score);

Of course, you’ll want to use

PlayerPrefs.GetInt("HighScore");

to make sure you actually achieved a new high score before you save it.

2 Likes

thank you very much