• 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
0
Question by UnityNoob123 · May 25, 2015 at 07:06 AM · scorehighscoresprefs

Saving Score In Player Prefs?

I have a score script that is functionally working using the canvas feature. It displays your score once you die for that run, but it doesn't show your highest score you have gotten. The scores are stored on two separate canvases with the text feature, the high score text is coming up with nothing, while the current score text is working perfectly fine. I have been working hard on this with lots of research and I still can't get it to work. Help would be greatly appreciated because I cannot figure it out. Here is my score manager script:

     public static int score;
 public static int highScore;
 public static int newHighScore;
 Text text;

 void Awake (){
     text = GetComponent<Text> ();
     score = 0;
 }
 void Update(){
     text.text = "" + score;
     if (score > highScore) {
         StoreHighScore();

     }
 }
     


 public static void AddPoints (int pointsToAdd){
 score += pointsToAdd;
 }

 public static void Reset(){
     score = 0;
 }

 void StoreHighScore(){

         PlayerPrefs.SetInt("highscore", highScore);
         print (PlayerPrefs.GetInt ("highscore", highScore));
         PlayerPrefs.Save ();
         score += newHighScore;
         
         
 }

}

and then my high score script:

Text text;

 void Awake(){
     text = GetComponent<Text> ();
 }
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
     text.text = "" + ScoreManager.newHighScore;
 }

}

My ScorePoint Script:

public class ScorePoint : MonoBehaviour { public int scoreValue = 1;

 void OnTriggerEnter2D (Collider2D col){
     if (col.GetComponent<PlayerController> () == null)
         return;
     ScoreManager.score += scoreValue;

 }

}

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by gabrielcac · May 25, 2015 at 01:25 PM

It seems to me that you forgot to update your highscrore value on your first script. Try this:

First, load the stored highscore into your highscore variable on your first script.

 void Awake (){
     text = GetComponent<Text> ();
     score = 0;
     highscore = PlayerPrefs.GetInt("highscore");
 }

On this same script, change your Update() function so it updates the value of the highscore.

 void Update(){
     text.text = "" + score;
     if (score > highScore) {
         highScore = score;
         StoreHighScore();
     }
 }

I don't think you need the newHighScore variable, so I would delete it. Also, remove the score += newHighScore; from your StoreHighScore() function.

On your HighScore script, change the Update() function to:

 void Update () {
     text.text = "" + ScoreManager.highScore;
 }

I believe this should make your scripts work.

I would like to add a sugestion to your script. In your first script you have an AddPoints() function, but on your ScorePoint class you ignore this function and directly update the score value. You should use the AddPoints() function, so inside this function you could already check if the new score is greater than the highscore and also update it if needed. This ensure that the highScore is always update when the new score is greaterthan it. By doing this you wouldn't need your Update() function any more, so you should remove it. There is even a performance gain with this solution.

To make an even better solution, try using private variables and use a property to access it.

Comment
Add comment · Show 1 · Share
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 UnityNoob123 · May 25, 2015 at 07:15 PM 0
Share

Thank you for the help that is exactly what I wanted to happen, you are awesome, and thanks for QuanArmy's response too.

avatar image
0

Answer by QUANARMY · May 25, 2015 at 06:40 PM

void Update(){ text.text = "" + score; if (score > highScore) { highScore = score; StoreHighScore();

  }

}

Comment
Add comment · Share
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
0

Answer by QUANARMY · May 26, 2015 at 06:14 AM

try this

void Update(){ text.text = "" + score; if (score > highScore) { highScore = score; StoreHighScore();

  }

}

Comment
Add comment · Show 1 · Share
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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Is there a decent tutorial for a local high score table (android)? 1 Answer

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

Creating score based on time with PlayerPrefs 1 Answer

Is it necessary to create an Array() to show high scores in PlayerPref? 1 Answer

PlayerPrefs not saving my variable value for HighScore System 3 Answers

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