Can someone tell me why my high score is not working?
using UnityEngine;
using System.Collections;
public class ScoreManager : MonoBehaviour {
public int score = 0;
public GUIStyle myGUIStyle;
public GUIStyle style;
int newHighscore;
void OnGUI() {
GUI.Label( new Rect(0,0, 100, 50), "Score : " + score, myGUIStyle);
GUI.Label( new Rect(0,0, 100, 50), "Highscore : " + newHighscore, style);
}
void StoreHighscore(int newHighscore)
{
int oldHighscore = PlayerPrefs.GetInt("highscore", 0);
if(score > oldHighscore)
PlayerPrefs.SetInt("highscore", newHighscore);
}
}
well in that script you’re not calling StoreHighscore ever… are you calling it anywhere else?
I fixed this using this script.
using UnityEngine;
using System.Collections;
public class ScoreManager : MonoBehaviour {
public int score = 0;
public GUIStyle myGUIStyle;
public GUIStyle style;
public int highScore;
string highScoreKey = "HighScore";
void Start(){
//Get the highScore from player prefs if it is there, 0 otherwise.
highScore = PlayerPrefs.GetInt(highScoreKey,0);
}
void Update () {
}
void OnGUI() {
GUI.Label( new Rect(0,0, 100, 50), "Score : " + score, myGUIStyle);
GUI.Label( new Rect(0,0, 100, 50), "Highscore : " + highScore, style);
}
void OnDisable(){
//If our score is greter than highscore, set new higscore and save.
if(score>highScore){
PlayerPrefs.SetInt(highScoreKey, score);
PlayerPrefs.Save();
}
}
}
The problem I am having now is, when I tested it I got a score of 9. Now when I build the game, my high score is 9.