Hi all I’m currently trying to implement highscores in a game I’m implementing. The scores page has the following script;
using UnityEngine;
using System.Collections;
public class Score : MonoBehaviour {
public static int score = 0;
void Update(){
guiText.text = "Score:" + score.ToString();
}
}
I’d like to make the score be available in a leaderboards menu item if they have beaten a high score otherwise the static score would be stored but I’m unsure how to implement this. I’ve read PlayerPrefs would be a good start but can’t find a tutorial for dummies after carrying out research. I’d like to display top 5 high scores in the leaderboards scene.
Here is a bare bones example.
using UnityEngine;
using System.Collections;
public class Score : MonoBehaviour {
public int score = 0;
public int highScore = 0;
string highScoreKey = "HighScore";
void Start(){
//Get the highScore from player prefs if it is there, 0 otherwise.
highScore = PlayerPrefs.GetInt(highScoreKey,0);
}
void Update(){
guiText.text = "Score:" + score.ToString();
}
void OnDisable(){
//If our scoree is greter than highscore, set new higscore and save.
if(score>highScore){
PlayerPrefs.SetInt(highScoreKey, score);
PlayerPrefs.Save();
}
}
}
Then a leaderBoard script.
using UnityEngine;
using System.Collections;
public class LeaderBoard : MonoBehaviour {
public int highScore;
string highScoreKey = "HighScore";
void Start(){
highScore = PlayerPrefs.GetInt(highScoreKey,0);
//use this value in whatever shows the leaderboard.
}
}
**The LeaderBoard script is in a different scene **