- Home /
How to save a high score
Hey so I have been working on this for like 2 hours now and still cant figure it out. Sorry to ask for code but I am desperate. So basically I have a 2d running game(like temple run, but 2d.), and I have a counter that counts up every time the level is loaded. It is placed in a GUI so it looks like a score. I need a way to save the best score you achieve and display it in a GUI as "High Score". I think you nee to use player prefs but I'm not sure. Thanks again. If you have any questions or if my question is confusing just comment and I'll get back to you. Thanks! Ill post the code for the counter.
var HighScore : int = 0;
var reset : int = 0;
var Counter : int = 0;
function Start () {
}
function OnGUI(){
guiText.text ="Score:"+ Counter;
}
function Update() {
Counter ++;
Debug.Log( Counter);
save ();
}
function OnLevelWasLoaded(level : int){
if( level == 0){ //My level number is 0
Counter = reset; //When the level is loaded reset the score/counter to 0
}
else
{
Counter ++;
}
}
function save () {
if( Counter >= HighScore){
HighScore = Counter;
PlayerPrefs.SetInt("High Score", HighScore);
print(PlayerPrefs.GetInt("High Score")); //Not sure if I did the player prefs right
} }
Your answer
Follow this Question
Related Questions
How can i get the following script back on track to being a single level high score saver 0 Answers
PlayerPrefs Highscore problem 1 Answer
high scores scene 2 Answers
saving highscores 1 Answer
Playerprefs not work!? 0 Answers