Issue with PlayerPrefs and local highscore

Hello I’m trying to use PlayerPrefs to save a score and then display it in another scene. I’m basing it off a script from another [question][1], but without the player being able to save their name. However I’m having some trouble getting it to work.
If it helps this is my point scoring script, I’ve named it ‘PointScoring’: Updated

private var powerUpAble = true;
var score = 0;
var DeductSlowDownScore = 65;
var scoreText = "Score: ";
var mySkin : GUISkin;
var saveScoreScript : SaveScore;

function Start(){
saveScoreScript = GetComponent(SaveScore);
}

function OnTriggerEnter( other : Collider ) {
    Debug.Log("OnTriggerEnter() was called");
    if (other.tag == "Coin") {
        Debug.Log("Other object is a coin");
        score += 115;
        Destroy(other.gameObject);
	} 
        else if (other.tag == "Rock") {
        Debug.Log("Other object is a rock");
        score = Mathf.Max(0, score - 75);
    }
     	else if (other.tag == "Tree") {
        Debug.Log("Other object is a tree");
        score = Mathf.Max(0, score - 50);
    }

    scoreText = "Score: " + score;
    Debug.Log("Score is now " + score);
	saveScoreScript.AddScore(score);
}

function OnGUI () {
	GUI.skin = mySkin;
    GUI.Box (Rect (140, 10, 500, 200), scoreText.ToString());
    if (powerUpAble)
    if (GUI.Button (Rect (5, 65, 110, 60), "Slow Down"))
        StartCoroutine( SlowDown( 1.5 ) );
 
 	}


function SlowDown(deactivateInSeconds : float)
{
    var script4 = GetComponent("Raft Forward - Easy"); 
    script4.enabled = false;
    var script5 = GetComponent("RaftForwardEasier - Easy"); 
    script5.enabled = true;
    var script6 = GetComponent("Pause Button - Easy"); 
    script6.enabled = false;
    powerUpAble = false;

    yield WaitForSeconds(deactivateInSeconds);

    script5.enabled = false;
    script4.enabled = true;
    script6.enabled = true;
	score = Mathf.Max(0, score - DeductSlowDownScore);
	scoreText = "Score: " + score;
    Debug.Log("Score is now " + score);
    powerUpAble = true;

}; 

Here is the script I’m using to save the score, which I’ve called ‘SaveScore’: Updated

    GetComponent("PointScoring");
    function AddScore(score : int){
       var newscore : int;
       var oldscore : int;
       newScore = score;
       for(i=0;i<10;i++){
          if(PlayerPrefs.HasKey(i+"HScore")){
             if(PlayerPrefs.GetInt(i+"HScore")<newScore){ 
                oldScore = PlayerPrefs.GetInt(i+"HScore");
                PlayerPrefs.SetInt(i+"HScore",newScore);
                newScore = oldScore;
             }
          }else{
             PlayerPrefs.SetInt(i+"HScore",newScore);
             newScore = 0;
          }
       }
    }

Both my point scoring script and the script I’m using to save the score are attached to the same gameobject in the same scene.
And lastly this is the script I’m trying to use to display the score in another, seperate scene. I’ve name it ‘DisplayScore’:

    var HighestScoreAchieved : int;
    
    function UpdateHScore(){
    
    	HighestScoreAchieved = PlayerPrefs.GetInt("0HScore");
    
    }

The problem is that the player’s score isn’t being recorded. In the scene where I want to display the score the inspector shows ‘Highest Score Achieved’ as 0, instead of what the score actually was. I’m not sure where I’m going wrong.

Any answers or feedback are greatly appreciated. -Ben
[1]: http://answers.unity3d.com/questions/20773/how-do-i-make-a-highscores-board.html

You are really making a mountain out of a molehill here buddy! There is a really easy way to do this, I had to do it for a university project and mine scored top marks :slight_smile: Here’s how to do it in less than 10 lines of code.

Step 1: Delete anything you have already included about PlayerPrefs and high scores, let’s work with a fresh base.

step 2: Locate the script that enables the Win Game state (maybe if you destroy all the targets the win game scene will be loaded). Once you have done this under the “Application.LoadLevel(winGame)” you need to include this but make it fit your code:

function GameOver()
{
		
		if (score > PlayerPrefs.GetInt("HighScore")){
		PlayerPrefs.SetInt("HighScore", score);
				
		}
               Application.LoadLevel(WinGame);
}

Basically this is telling the program to save the variable score as HighScore. This will be consistent even if the application is closed. PlayerPrefs are great!

Step 3: Create a final script named anything, I named mine HighScoreDisplay and include this:

var hScore: TextMesh; //Declares a text mesh

function Awake(){

hScore.text = "High Score: " + PlayerPrefs.GetInt("HighScore");
}

You’ll be glad to know that’s all the script it takes to get this working! You need to attatch the latter script to a text mesh in your win game scene. Once you have attached it, drag the text mesh from the hierarchy to the variable slot (HighScoreDisplay) and see what you get!

Let me know how you get on.

you need to call AddScore() in order to send it a score… Where are you doing that? There are a few weird things here besides:

the first line:

GetComponent("SaveScore");

instead, we can declare a var of that type… (use the Start function to initialize it). remove that line, and add these lines to “PointScoring”:

var saveScoreScript : SaveScore;

function Start(){
saveScoreScript = GetComponent(SaveScore);
}

… Also, this line:

print(AddScore);

not sure what that’s meant to be… but it’s not even inside any function, so it won’t be called at any rate…

To call AddScore, you could add this line to the (updated) PointScoring script:

 ...
scoreText = "Score: " + score;
Debug.Log("Score is now " + score);
saveScoreScript.AddScore(score);    //add this line
}

so here would be the updated script:

private var powerUpAble = true;
var score = 0;
var DeductSlowDownScore = 65;
var scoreText = "Score: ";
var mySkin : GUISkin;
var saveScoreScript : SaveScore;

function Start(){
saveScoreScript = GetComponent(SaveScore);
}

function OnTriggerEnter( other : Collider ) {
    Debug.Log("OnTriggerEnter() was called");
    if (other.tag == "Coin") {
        Debug.Log("Other object is a coin");
        score += 115;
        Destroy(other.gameObject);
    } 
        else if (other.tag == "Rock") {
        Debug.Log("Other object is a rock");
        score = Mathf.Max(0, score - 75);
    }
        else if (other.tag == "Tree") {
        Debug.Log("Other object is a tree");
        score = Mathf.Max(0, score - 50);
    }

    scoreText = "Score: " + score;
    Debug.Log("Score is now " + score);
    saveScoreScript.AddScore(score);

}
... etc

EDIT:

Update–
The first script is good now, don’t mess with it (except you can delete the stray semi-colon at the end)

The SECOND script is still problematic…
Let’s just try to simplify things:

GetComponent("PointScoring");  // this line does nothing, delete it!
function AddScore(score : int){
         if(PlayerPrefs.GetInt("High Score") < score)
            PlayerPrefs.SetInt("High Score",score);
  }

then of course the third script looks like:

var HighestScoreAchieved : int;

function Update(){
    HighestScoreAchieved = PlayerPrefs.GetInt("High Score");
}

simple as that

Adding on to Nercoe, you can do it as gui text aswell.

var scores : GUIText;

function Awake(){

scores.text = "High Score: " + PlayerPrefs.GetInt("HighScore");

}