Hello I’m trying to use PlayerPrefs to save a score and then display it in another scene, as ‘Highest score achieved: …’ I’m basing it off a script from another question, but without the player being able to save their name. However I’m having some trouble getting it to work. Here is the script I’m using to save the score, attached the same game object as my point scoring script:
And here is the script used to display the score in another scene:
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 would be greatly appreciated. -Ben
You have two or three post with the same question. All you have to do is bump the topic back to the top of the list by posting again in the same thread.
There are still many unknowns here. Who’s “Start” function are you calling the “UpdateHScore” function from? Are they both in the same script that you have attached to the gameobject? Can you post more of the code for the game object to make it easier to see what’s going on? That would be really helpful to see what’s up.
My apologies. This is my point scoring script, I should have included this in the question:
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;
};
This point scoring script and the separate script I’m using the save the score are attached to the same, single, gameobject in one scene. Then in a separate scene is the script I’m trying to use to display the score, attached to a camera.
I hope you’ll bear with me. I usually code in C#, so I could be missing something. I have a few questions…
Where is “score” in the point scoring script being declared?
Since the point scoring script and the first script you posted are on the same object, is there a reason you have the score saving function in a separate script?
Since the score keeping and the score saving are in different scripts, there needs to be something in the point scoring code to reference the script that actually saves the score. Is this last script you posted in its entirety, or is there a reference to the other script missing from the code there?
Just asking questions to try and help put the puzzle together and be helpful is all.
How silly of me, I didn’t post the whole script. It’s been fixed above. I believe the score is being declared in the second line: ‘var score = 0;’
There’s isn’t a particular reason why I have both the point scoring and the PlayerPrefs coding in separate scripts, I just thought it might be a little easier to manage.
I should have thought of that, thank you. I’ve changed it so now both my point-scoring script and score-saving scripts reference each other. But the player’s score still isn’t being displayed in the inspector for my script used to display the score. Looks like I might have to go back to the drawing board on this one,