- Home /
Player prefs easy highscore?
I have read many pages on here about playerprefs, but what I'm thinking should work is not working. I want to set the highscore to 500 if there isn't a highscore in the player prefs already. The highscore will be saved as "monday1" in the prefs.
It seems so simple, but its not working. Please help.
#pragma strict
var highScore: int;
function Awake()
{
if (!PlayerPrefs.HasKey("monday1"))
{
Debug.Log("there is no key, it is now set to 500!");
PlayerPrefs.SetInt("monday1", 500);
}
}
function Start()
{
highScore = PlayerPrefs.GetInt("monday1");
guiText.text = "High Score: " + highScore;
}
The code looks fine, and in a quick test it worked fine. Note it needs to be attached to a GUIText game object. What is the specific problem you are having?
It's not working. And it is attached to the GUITexture that says highscore +the variable. Did it say highscore 500 for you? When I run it, it says highscore: 0
it should say highscore: 500 since that variable doesn't exist in the playerprefs
It says 'HighScore: 500'. Start with a new scene, create a GUIText object. Add your script above, and hit play (might change the key just to check everything out). Do you still have problems?
I will try it with a new scene in a little while, and get back to you guys. Thanks for all the help guys
Answer by Narv · Aug 12, 2013 at 06:04 PM
Your question is a bit vague but I assume that you aren't getting the highScore value to equal 500? Did you try using PlayerPrefs.Save() to make sure it saves properly?
PlayerPrefs.Save(); is called automatically when the scene changes or when the application quits.
Correct, that's my problem. Do I need to call PlayerPrefs.Save() if im not changing the scene?
Thanks for the down vote @lancer on telling me something happens with the application quits and he isn't quitting the application....
From personal experience I know I had to do a PlayerPrefs.Save() to fix a playerprefs error in my scene.. It was a suggestion, but thanks for being useless about it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613112044im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
PlayerPrefs save after quit? 2 Answers
PlayerPrefs Highscore problem 1 Answer
PlayerPrefs highscore system? 1 Answer
Implementing HighScore board 0 Answers
How do I get my endless runner high score,How do I make high score for an endless runner 1 Answer