- Home /
 
Save and Retrieve Score not working!
Dear all, I have been encountering a very irritating problem when creating my first app. I am trying to use the playerpref to save my score, but when it restarts, the highscore is reset to 0. I am an absolute beginner, so please treat me like a dunce. Ill paste the 2 scripts I have for scoring a point on trigger, and the other is the general score system. I have tried this on my phone, and whenever I score a point and update the score, the highscore is 0. Many thanks for the help :) from fatmanspineapple For the score point using UnityEngine; using System.Collections;
 public class ScorePoint : MonoBehaviour {
 
     void OnTriggerEnter2D(Collider2D collider){
         if(collider.tag == "Player"){
             ScoreSystem.AddPoint();
             gameObject.SetActive(false);
         }
 
 
 }
     }
 
               For the score system
 using UnityEngine;
 using System.Collections;
 
 public class ScoreSystem : MonoBehaviour {
 
     static int score = 0;
     static int highScore = 0;
     static ScoreSystem instance;
 
     static public void AddPoint(){
                 score++;
 
                 if (score > highScore) {
                         highScore = score;
                 }
         }
 
 
     void Start() {
         PlayerPrefs.GetInt("highScore", 0);
 
 
     }
     void OnDestroy(){
         PlayerPrefs.SetInt ("highScore", highScore);
         score = 0;
 
           }
                          
     void Update () {
         guiText.text = "S:" + score +  "\nHS:" + highScore;
     
     }
 }
 
              You need to save your PlayerPrefs in order for the changes to be made to the disc. Use PlayerPrefs.Save();
  void OnDestroy(){
          PlayerPrefs.SetInt ("highScore", highScore);
          // This line is important
          PlayerPrefs.Save();
         score = 0;
  
            }
 
                 I was also wondering whether there was a code I could use to display this highscore on a different screen? After the game object collides with a killing game object, a screen is loaded and this highscore is displayed.
Dear Harshad, I tried this on my phone. The highscore saves but when I close the app and then reopen it, the high score resets to 0 again. i have tried everything and Im not sure what to do now
Answer by gjf · Jan 15, 2015 at 01:35 PM
you never initialize highScore from player prefs - line 20:
 PlayerPrefs.GetInt("highScore", 0);
 
               should be
 highScore = PlayerPrefs.GetInt("highScore", 0);
 
              Your answer
 
             Follow this Question
Related Questions
PlayerPrefs Problem crashing with SetBool 2 Answers
How to use PlayerPrefs? 2 Answers
Save level 3 Answers
How to save resources configuration file for unity3d? 0 Answers
Saving Location in Unity 1 Answer