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:
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
Finally I fixed it. It’s pretty logic and simple now.
private var timeSurvived : float;
var highScore = PlayerPrefs.GetFloat("highScore");
timeSurvived = PlayerPrefs.GetFloat("timeSurvived");
if (timeSurvived > highScore)
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.
Follow a similar structure to prevent new highscores being set all the time.
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:
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;
pHighScore = scoringObject.GetComponent(scoringScript).playerHighScore;
if ( pHighScore >= highScore )
highScore = pHighScore;