Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

I made a better shader how do i fix[add _Shadow Strength]help???>Sorry that im asking for to much 1 Answer

How many different scripts do I need to write to create a highscore system? 1 Answer

Distance and Best Distance 1 Answer

my code is suppose to store high score and its storing recent score insted and it wont stop if game over and it has an score counter as well plzz help 0 Answers

high scores scene 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges