Hello I’m trying to use this script to save a player’s high score, called ‘SaveScore’:
GetComponent("PointScoringScript");
function GameOver()
{
if (score < PlayerPrefs.GetInt("HighScore")){
PlayerPrefs.SetInt("HighScore", score);
}
Application.LoadLevel (1);
}
To display the score I’m using this script, in a different scene, called ShowScore:
var hScore: TextMesh;
function Awake(){
hScore.text = "High Score: " + PlayerPrefs.GetInt("HighScore");
}
And lastly this is the point scoring script I’m using in conjunction with the PlayerPrefs script. This script is in the same scene, and attached to the same gameobject as the PlayerPrefs script:
GetComponent("SaveScore");
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 ) );
}
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;
};
‘score’ in the SaveScore script is an unknown identifier so I added the line ‘var score: int;’. This got rid of the problem but the player’s score isn’t being recorded and shown in the High Score scene like it’s intended to.
If it helps the Score variable in the SaveScore script constantly reads as 0 in the Inspector even when the score is updated in the point scoring script. I’m not sure where to go from here.
Thank you for any answers or feedback. - Ben