- Home /
PlayerPrefs not saving my variable value for HighScore System
Ok, so I have this code in my restart scene for setting up a highScore variable, if its bigger than the previous one.
private var scoreSaver : float;// Time of survive
private var playerHighScore : float; // Time of survive for highscore uses
private var storeHighScore : float = 0;
function Start ()
{
scoreSaver = PlayerPrefs.GetFloat("Time Survived"); //Retrieves my survive time of previous code(I've set up it correctly in other code)
playerHighScore = PlayerPrefs.GetFloat("High Score"); Retrieves my survive time of previous code. Equals above, but I need two of this timer, because one of them resets every restart button click.
if (scoreSaver >= playerHighScore && scoreSaver >= storeHighScore)
{
storeHighScore = playerHighScore;
}
}
In the restart button, I have this:
PlayerPrefs.DeleteKey("Time Survived");
The problem is that every time I play, the storeHighScore doesn't get its value, it gets reseted, so I'm always having a new highScore, event if its lower than the previous one
When/where are you assigning storeHighScore back to PlayerPrefs?
PlayerPrefs.SetFloat( "High Score", storeHighScore );
http://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.SetFloat.html
In a OnGUI Function of other scene and other script.
PlayerPrefs.SetFloat("Time Survived", difficultyTimer); //SAVES PLAYER SURVIVE TI$$anonymous$$E
PlayerPrefs.SetFloat("High Score", difficultyTimer);
Answer by MarceloTerreiro · Aug 19, 2013 at 08:58 PM
Finally I fixed it. It's pretty logic and simple now.
private var timeSurvived : float;
function Awake()
{
var highScore = PlayerPrefs.GetFloat("highScore");
timeSurvived = PlayerPrefs.GetFloat("timeSurvived");
if (timeSurvived > highScore)
{
PlayerPrefs.SetFloat("highScore", timeSurvived);
}
}
My problem was assigning the highScore variable, but I've discovered I needed to make it equals to PlayerPref highScore float. After that, I did not have any other problems. This also doesn't creates multiple playerPreference.
Answer by ThomasB · Aug 19, 2013 at 07:28 PM
if(PlayerPrefs.HasKey("HScore"))
{
if(PlayerPrefs.GetInt("HScore")<curScore)
{
PlayerPrefs.SetInt("HScore", curScore);
}
}else{
PlayerPrefs.SetInt("HScore",curScore);
}
Follow a similar structure to prevent new highscores being set all the time.
Answer by meat5000 · Aug 19, 2013 at 07:43 PM
Make a separate gameObject called HighScoreKeeper or something with a script which reads the
public var playerHighScore
from your above script.
if you have something like on whatever is keeping score:
function Update()
{
currentScore = //whatever;
if ( currentScore >= highScoreKeeper.GetComponent(highScoreScript).highScore )
(
playerHighScore = currentScore;
}
}
then in HighScoreKeeper object have something like:
public var highScore : int = 0; var pHighScore : int = 0;
function Update()
{
pHighScore = scoringObject.GetComponent(scoringScript).playerHighScore;
if ( pHighScore >= highScore )
{
highScore = pHighScore;
}
}