- Home /
Save Timer HighScore
I have a script, which has a timer and i want to make a highscore system for that timer. I have already made a script but it won't save the highscore. It only updates the score, so if the score is 45 seconds, the best will be 45 seconds and then if it is 65 seconds the next time, it will update to 65 seconds. But it isn't supposed to, because 65 seconds is slower than 45 seconds. Anyone who knows what is wrong?
Here is the script.
var bestTime : int = 0;
var completeTime : int = 0;
static var gameTimeAllowed : float = 60.0;
static var gameTimeRemaining : float = gameTimeAllowed;
function Start() {
bestTime = PlayerPrefs.GetInt("bestTime",bestTime);
}
function Update() {
completeTime = gameTimeAllowed - gameTimeRemaining;
if(bestTime < completeTime){
bestTime = completeTime;
PlayerPrefs.SetInt("bestTime",bestTime);
}
}
function OnGUI() {
GUI.Label(Rect(Screen.width/2-80,Screen.height/2-130,300,100),"Best Time : " + bestTime.ToString() + " Seconds");
}
Answer by MrSoad · Nov 09, 2014 at 02:04 AM
Change :
if(bestTime < completeTime){
to :
if (bestTime > completeTime) {
Your evaluation is the wrong way round, so whenever completeTime is more than the best time you are setting it to be the best time at the moment.
I appreciate you help, but now it just keeps saying the best score is 0?
If you give it a starting value of 0 then you have given it the best score you can ever get right from the start. Give it your maximum allowed time for the level as it's starting value.
var bestTime : int = 400;
You will need to delete your current player pref for bestTime as well, or it will continue to override your setting with an unbeatable 0.