How do I save my Highest time using playerprefs?
Hi,
I'm currently working on a endless runner game and im struggling with my high score. I have a timer script that works just fine, but I want to able to save the highest score using playerprefs. Does anyone have a clue on how I can do this?
Here's my current timer script:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Timer : MonoBehaviour {
public Text timer;
public float highscore = 0;
public float seconds = 00;
public float miliseconds = 00;
static public float minutes = 00;
public Canvas canvas;
void FixedUpdate(){
if (miliseconds >= 60) {
seconds++;
miliseconds = 00;
}
if (seconds >=60) {
minutes++;
seconds = 00;
}
if (GameOver.gameover == false) {
miliseconds += Time.deltaTime * 60;
}
timer.text = minutes.ToString ("00") + ":" + seconds.ToString ("00") + ":" + miliseconds.ToString ("00");
}
}
Try searching for the term "PlayerPrefs" using the search box above, there are plenty of examples already in these pages.
Answer by JScotty · Oct 05, 2016 at 03:15 PM
Hi Joey,
You can save your highscore time just by doing :
PlayerPrefs.SetInt("Minutes", minutes);
PlayerPrefs.SetInt("Seconds", seconds);
PlayerPrefs.SetInt("Miliseconds", miliseconds);
after you've done that, and you want to check if your new score is bigger than your saved score do:
int highscoreMins = PlayerPrefs.GetInt("Minutes");
int highscoreSec = PlayerPrefs.GetInt("Seconds");
int highscoreMilisec = PlayerPrefs.GetInt("Miliseconds");
//compare those with your old ones
if(minutes > highscoreMins){
//new highscore
SetNewHighscore();
} else if(minutes == highscoreMins){
if(seconds > highscoreMins){
//new highscore
SetNewHighscore();
}
if(seconds == highscoreSec){
if(miliseconds > highscoreMilisec){
//new highscore
SetNewHighscore();
} else {
//do nothing
}
}
}
void SetNewHighscore(){
PlayerPrefs.SetInt("Minutes", minutes);
PlayerPrefs.SetInt("Seconds", seconds);
PlayerPrefs.SetInt("Miliseconds", miliseconds);
}
hope it helped a bit more info: https://docs.unity3d.com/ScriptReference/PlayerPrefs.html
I also recommend to use const strings to declare your saveData names like:
const string $$anonymous$$utesPlayerPrefs = "$$anonymous$$inutes";
const string secondsPlayerPrefs = "Seconds";
const string milisecondsPlayerPrefs = "$$anonymous$$iliseconds";
and then when you try to get or set your prefs use:
PlayerPrefs.SetInt($$anonymous$$utesPlayerPrefs , $$anonymous$$utes); // set $$anonymous$$utes
int highscore$$anonymous$$inutes = PlayerPrefs.Getint($$anonymous$$utesPlayerPrefs); // get $$anonymous$$utes
PlayerPrefs.SetInt(secondsPlayerPrefs , seconds); // set seconds
int highscoreSeconds = PlayerPrefs.Getint(secondsPlayerPrefs); // get seconds
PlayerPrefs.SetInt(milisecondsPlayerPrefs , miliseconds); // set miliseconds
int highscore$$anonymous$$iliseconds = PlayerPrefs.Getint(milisecondsPlayerPrefs); // get miliseconds
to prevent misspelling, and errors.
You save a playerpref by doing PlayerPrefs.SetInt("$$anonymous$$inutes", $$anonymous$$utes);
The changes you make using the Set functions will not persist unless you subsequently call PlayerPrefs.Save()
It's more common to convert to a single unit, like only seconds. $$anonymous$$akes the code faster and less error-prone. For example, there's a small bug where you compare seconds to $$anonymous$$utes. Not completely on you, since the OP is already using the not-so-good seconds+$$anonymous$$utes way.
The conversion to $$anonymous$$utes/seconds/etc is generally then done only when displaying to the human.