PlayerPrefs.GetInt does not show up

Hi guys, could you please tell me what is wrong with my script? If I set it at a normal integer like 5 it does show up in the text, but I want the highscore to show up. Probably I overlooked something again. Thanks in advance :slight_smile:

    public Text scoreText;
public Text highscoreText;

int score;
int oldHighscore;

void Start () 
{
	score = 0;
	oldHighscore = PlayerPrefs.GetInt("highscore", 0); 
	InvokeRepeating	("ScoreUpdate", 1, 0.5f);
}

void Update () 
{
	scoreText.text = "Score: " + score;
	highscoreText.text = "Highscore: " + PlayerPrefs.GetInt("highscore");

	if (score > oldHighscore) 
	{
		PlayerPrefs.SetInt ("highscore", score);
		oldHighscore = score;
		PlayerPrefs.Save ();
	}
}

void ScoreUpdate ()
{
	score += 1;
}	

}

public Text scoreText; public Text highscoreText; int score; int highscore; void Start () { score = 0; InvokeRepeating ("ScoreUpdate", 1, 0.5f); highscore = PlayerPrefs.GetInt ("High Score"); } void Update () { scoreText.text = "Score: " + score; highscoreText.text = "Highscore: " + highscore; } void ScoreUpdate () { score += 1; if (score > highscore) { PlayerPrefs.SetInt("High Score", score); PlayerPrefs.Save (); } } I tried a different code , still does not work

–

1 Answer

1

Your first one should work just fine. Your second one doesn’t work since you never update “highscore”. You only read it once at the start of your game. You need to update it inside the ScoreUpdate method whenever your “score” is higher than “highscore”.

Okay thanks it works now . :)

–