# Having Trouble holding PlayerPrefs after game restart

I am trying to create a highscore value that is stored in playerprefs, and only updates when a higher score is achieved. I got playerprefs viewer on the asset store, and can see that it is holding a value, but it seems that it blows through the if statement that checks to see if the new value is a highscore. This means that no matter what score I get, it always stores it as a highscore.

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

public class Score : MonoBehaviour
{
public Transform Player;

public Text scoreText, Highscoretext;

public GameObject Postgame, Ingame;

private bool stopScore = false;

void Update()
{
if (stopScore == false)
{
//Keeps track of and displays current score
float floatscore = Player.position.x;
int intscore = (int)floatscore / 10;
if (FollowPlayer.gameOver == false)
{
scoreText.text = intscore.ToString();
}

if (FollowPlayer.gameOver == true)   // Checks wheather the game has ended (from a different script)
{
stopScore = true;   // Stops running the score function

if (intscore > (PlayerPrefs.GetInt("HighScore")));   // Checks to see if the score of the current game is a new Highscore
{
PlayerPrefs.SetInt("HighScore", intscore);   // Sets new Highscore

EndScreen();   // Function to load the endscreen
}
}
}
}
public void EndScreen()
{

Highscoretext.text = "Highscore: " + PlayerPrefs.GetInt("HighScore").ToString();   // Supposed to set the endscreen Highscore value to the new Highscore value

// Different canvas overlays
Postgame.gameObject.SetActive(true);
Ingame.gameObject.SetActive(false);
}
}


I tried using Debug.Log to debug this, and found that even if intscore is smaller than PlayerPrefs.GetInt("HighScore")), it will still run the code that sets a new highscore. Anyone have any idea of how to fix this?

• Did you try to set breakpoints to see what is exactly happening? Another advice, try to dont run this code on Update. You can make an event called when the game is over. Subscribe to this event and call your score saving code. – Ben Jun 15 '20 at 11:27

if (intscore > (PlayerPrefs.GetInt("HighScore")));