Saving Score In Player Prefs?

I have a score script that is functionally working using the canvas feature. It displays your score once you die for that run, but it doesn’t show your highest score you have gotten. The scores are stored on two separate canvases with the text feature, the high score text is coming up with nothing, while the current score text is working perfectly fine. I have been working hard on this with lots of research and I still can’t get it to work. Help would be greatly appreciated because I cannot figure it out. Here is my score manager script:

    public static int score;
public static int highScore;
public static int newHighScore;
Text text;

void Awake (){
	text = GetComponent<Text> ();
	score = 0;
}
void Update(){
	text.text = "" + score;
	if (score > highScore) {
		StoreHighScore();

	}
}
	

public static void AddPoints (int pointsToAdd){
score += pointsToAdd;
}

public static void Reset(){
	score = 0;
}

void StoreHighScore(){

		PlayerPrefs.SetInt("highscore", highScore);
		print (PlayerPrefs.GetInt ("highscore", highScore));
		PlayerPrefs.Save ();
		score += newHighScore;
		
		
}

}

and then my high score script:

Text text;

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

}

// Update is called once per frame
void Update () {
	text.text = "" + ScoreManager.newHighScore;
}

}

My ScorePoint Script:

public class ScorePoint : MonoBehaviour {
public int scoreValue = 1;

void OnTriggerEnter2D (Collider2D col){
	if (col.GetComponent<PlayerController> () == null)
		return;
	ScoreManager.score += scoreValue;

}

}

It seems to me that you forgot to update your highscrore value on your first script. Try this:

First, load the stored highscore into your highscore variable on your first script.

void Awake (){
    text = GetComponent<Text> ();
    score = 0;
    highscore = PlayerPrefs.GetInt("highscore");
}

On this same script, change your Update() function so it updates the value of the highscore.

void Update(){
    text.text = "" + score;
    if (score > highScore) {
        highScore = score;
        StoreHighScore();
    }
}

I don’t think you need the newHighScore variable, so I would delete it. Also, remove the score += newHighScore; from your StoreHighScore() function.

On your HighScore script, change the Update() function to:

void Update () {
    text.text = "" + ScoreManager.highScore;
}

I believe this should make your scripts work.

I would like to add a sugestion to your script. In your first script you have an AddPoints() function, but on your ScorePoint class you ignore this function and directly update the score value. You should use the AddPoints() function, so inside this function you could already check if the new score is greater than the highscore and also update it if needed. This ensure that the highScore is always update when the new score is greaterthan it. By doing this you wouldn’t need your Update() function any more, so you should remove it. There is even a performance gain with this solution.

To make an even better solution, try using private variables and use a property to access it.

void Update(){
text.text = “” + score;
if (score > highScore) {
highScore = score;
StoreHighScore();

 }

}

try this

void Update(){
text.text = “” + score;
if (score > highScore) {
highScore = score;
StoreHighScore();

 }

}