- Home /
Question by
AminSB00 · Feb 16, 2016 at 01:00 PM ·
playerprefsscorehighscore
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 :)
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;
}
}
Comment
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
Best Answer
Answer by Bunny83 · Feb 17, 2016 at 03:01 AM
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".