• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by RichCoggin · Oct 17, 2013 at 03:50 PM · playerprefsscorehighscores

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

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Oct 17, 2013 at 07:42 PM 0
Share

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?

avatar image RichCoggin · Oct 17, 2013 at 09:42 PM 0
Share

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?

avatar image chillersanim · Oct 17, 2013 at 09:53 PM 0
Share

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

avatar image robertbu · Oct 17, 2013 at 10:41 PM 0
Share

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.

avatar image RyanZimmerman87 · Oct 17, 2013 at 10:58 PM 0
Share

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.

Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

How do I save highscore (Very Simple) 3 Answers

PlayerPrefs HighScore problems, it doesn't work. 4 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges