Question by
El-Deiablo · May 14, 2016 at 08:38 PM ·
loadingsavingscene-switchinghighscores
Loading and Saving High Score
I have been working on this for hours, watched numerous tutorials, and searched unity answers, but cannot figure out what I'm doing wrong. I want to save my highscore using player prefs, but when I refresh the game the high score is back to 0. I am using two scripts:
Code#1:
using UnityEngine;
using System.Collections;
using UnityEngine .UI;
using UnityEngine .SceneManagement ;
public class GAMEOVERManager : MonoBehaviour {
public Text highScoreText;
public Text scoreText;
public Text goldTeethText;
// Use this for initialization
void Start () {
highScoreText.text = PlayerPrefs.GetInt ("High Score").ToString ();
scoreText.text =PlayerPrefs.GetInt ("Score").ToString() ;
goldTeethText.text = PlayerPrefs.GetFloat ("Tooth Score").ToString ();
PlayerPrefs.Save ();
}
// Update is called once per frame
void Update () {
}
public void RestartGame(){
SceneManager.LoadScene ("Gameplay");
}
}
Code#2:
using UnityEngine;
using System.Collections;
using UnityEngine .UI;
using UnityEngine .SceneManagement ;
public class ScoreManager : MonoBehaviour {
public static int highScoreCount;
public static int scoreCount;
public static int TKOCount;
public static float toothCount;
public Text TKOText;
public Text scoreText;
// Use this for initialization
void Start () {
PlayerPrefs.GetInt ("High Score");
IntializeVariables ();
TKOCount = 0;
scoreCount = 0;
}
// Update is called once per frame
void Update () {
if (scoreCount <= 0) {
scoreCount = 0;
}
if (scoreCount > highScoreCount) {
highScoreCount = scoreCount;
SaveHighScore ();
}
scoreText.text = " " + scoreCount;
TKOText.text = " " + TKOCount;
toothCount = scoreCount * .10f;
SaveScore ();
SaveGoldTeeth ();
}
void SaveTeeth(float teeth){
}
void SaveHighScore(){
PlayerPrefs.SetInt ("High Score", highScoreCount);
PlayerPrefs.Save ();
}
void SaveScore(){
PlayerPrefs.SetInt ("Score", scoreCount);
}
void SaveGoldTeeth(){
PlayerPrefs.SetFloat ("Tooth Score", toothCount);
PlayerPrefs.Save ();
}
void IntializeVariables(){
if (!PlayerPrefs.HasKey ("Game Initialized")) {
PlayerPrefs.SetInt ("Score", 0);
PlayerPrefs.SetInt ("High Score", 0);
PlayerPrefs.SetFloat ("Tooth Score", 0);
PlayerPrefs.SetInt ("Game Initialized", 123);
}
}
}
Any help is appreciated!
Comment
Best Answer
Answer by Louis-LCM · May 15, 2016 at 12:24 PM
void Start(){
highScoreCount= PlayerPrefs.GetInt("HighScore");
}
And when u check ur score :
if(scoreCount>highScoreCount){
PlayerPrefs.SetInt("HighScore", scoreCount);
PlayerPrefs.Save();
}