Hello I’m trying to save the player’s high score and display it another scene, using PlayerPrefs. Doing some searching I came across a local high score script but I’m not sure on how to incorporate it into my project correctly.
If it helps here is my scoring script, called ‘Score - Easy’:
private var powerUpAble = true;
var score = 0;
var DeductSlowDownScore = 65;
var scoreText = "Score: ";
var mySkin : GUISkin;
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);
}
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 ) );// deactivate for 1.5 secs
}
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;
};
Attached to the same object that my scoring script is attached to is the local high score script I tried to use. I named the script ‘SaveScore’:
GetComponent("Score - Easy");
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){
// new score is higher than the stored score
oldScore = PlayerPrefs.GetInt(i+"HScore");
PlayerPrefs.SetInt(i+"HScore",newScore);
newScore = oldScore;
}
}else{
PlayerPrefs.SetInt(i+"HScore",newScore);
newScore = 0;
}
}
}
It was also said to initialise the high score call ‘AddScore’ at the start of the game, but I’m not sure how to do so.
And to display the score I tried to use this script, in another scene, called ‘ShowScore’:
var 1stScore : int;
function UpdateHScore(){
1stScore = PlayerPrefs.GetInt(0HScore);
}
The problem is is that I can’t get it to work properly. The console is saying that the ‘1’ in ‘var 1stScore: int;’ is an unexpected token and I don’t know what to do.
Thank you greatly for any answers or feedback
-Ben