- Home /
Recognising high score when updated with a graphic
Hi there,
I hope you can help. I'm putting together a game which uses a scoring system. Basically as you go through the level/s, score is saved via PlayerPrefs like this:
if(chapter1Score > PlayerPrefs.GetInt("chapter1Score")){
PlayerPrefs.SetInt("chapter1Score", chapter1Score);
}
This all works fine. But what I want to do, is when a higher score is reached (from 40 to 50 say), it displays 'New Highscore!" At present, when you play a level in one scene, the score is recorded. When you go to another scene, where the score is displayed (the menu if you like) you can see the increase in score.
I know how to set all these things up, but can't quite get my head around it recognising this higher score. Another point to note is that the higher score - when playing the level is saved in that scene. ALso, I have tried to manipulate the above code like this but comes up with an error:
if(chapter1Score > PlayerPrefs.GetInt("chapter1Score")){
Debug.Log("New High Score");
}
Any help or guidance would be much appreciated. Thanks
I'm not sure where your issue is, but it is not with the code you've posted. To to be sure I copied your code into a script:
pragma strict
var chapter1Score = 0;
function Start () {
PlayerPrefs.SetInt("chapter1Score", chapter1Score);
}
function Update() {
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) {
chapter1Score++;
if(chapter1Score > PlayerPrefs.GetInt("chapter1Score")) {
Debug.Log("New High Score");
PlayerPrefs.SetInt("chapter1Score", chapter1Score);
}
}
}
It works fine. What error did you get when you use your second code fragment?
I'm not in front of it right now, but basically it referenced this line:
if(chapter1Score > PlayerPrefs.GetInt("chapter1Score"))
saying that the > was incorrect.
I notoced you've added "chapter1Score++;". does that make a difference?
The chapter1Score++ seems to be only for testing reasons.
Could you perhaps explain, what exactly is the error and behaviour that you are getting?
After displaying the "Got new score", you should also save the new score, because it doesn't create a reference to the save location, ins$$anonymous$$d it creates a local copy.
Greetings Chillersanim
As @chillersanim says, the chapter1Score++ was just there for testing purposes. It increases the score so the 'if' fires. Your line of code is fine. Something else is going on. Post the script that is generating the error to the list and it is highly likely someone can spot the issue.
You probably aren't going to want to compare against PlayerPrefs every update frame. It'd be better to use a normal int which stores the playerPref at start. Compare against the normal int and then when your value is bigger at that point update the playerpref and set your int to the new playerpref value.
That's more just a performance tip though not going to solve your problem entirely. Your code looks like it should work fine likely something else.
$$anonymous$$aybe you just need to use Start() to retrieve your player score for the new scenes?
void Start()
{
chapter1Score = PlayerPrefs.GetInt("chapter1Score", 0);
}
That will give chapter1Score your PlayerPref value correctly every time ins$$anonymous$$d of being 0 every time you go to a new scene.
In this example chapter1Score will only be equal to 0 if you never set it or if you set it to 0.
$$anonymous$$aybe that's the problem? Hard to say.
Your answer
Follow this Question
Related Questions
PlayerPref set int and get int 1 Answer
Is it necessary to create an Array() to show high scores in PlayerPref? 1 Answer
Highscoring and changing scripts (Java/UnityScript) 1 Answer
How many different scripts do I need to write to create a highscore system? 1 Answer
Taking high score from anther script and then displaying it 2 Answers