Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 alexapavlovich · Jul 14, 2017 at 10:20 PM · unity 5unity5scorescore systemhighscore

Storing high score

 public class Score : MonoBehaviour {
 
     ArcadeScore scoreSys;
 
     void Start()
     {
         GameObject obj = GameObject.Find("ScoreSystem");
         scoreSys = obj.GetComponent<ArcadeScore>();
     }
 
     void Update() {
         if (scoreSys.score == 0) {
             PlayerPrefs.SetInt("arcadeScore", scoreSys.score);
             PlayerPrefs.Save();
         }
     }
 
     void OnTriggerEnter2D(Collider2D coll)
     {
         if (coll.tag == "Ball")
         {
             scoreSys.score++;
             PlayerPrefs.SetInt("arcadeScore", scoreSys.score);
             PlayerPrefs.Save();
         }
     }
 }

The code above is used to detect collision and increase the score value

 public class ArcadeScore : MonoBehaviour {
 
     public int score, cs;
 
     void Start()
     {
         cs = PlayerPrefs.GetInt("arcadeHighscore");
         score = 0;
     }
 
     void Update() {
         if (score > cs) {
             PlayerPrefs.SetInt("arcadeHighscore", score);
             PlayerPrefs.Save();
         }
     }
 }

This script is supposed to increase score and in case the new score is greater than the old one, that should be the new highscore. The problem I have is that every time when I try to set the value cs to the latest score, it always sets to 0 and my highscore doesn't work. How can I fix that?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by FlaSh-G · Jul 15, 2017 at 08:08 AM

You set the PlayerPrefs value in two places:

 if (score > cs) {
   PlayerPrefs.SetInt("arcadeHighscore", score);

Here, you check if the score is actually higher.

But you also set the value here:

 scoreSys.score++;
 PlayerPrefs.SetInt("arcadeScore", scoreSys.score);

without checking whether the new score is actually higher. You need to remove this bit.

While we're at it, there is no need to have the ArcadeScore component check every frame whether or not the score is a new high score. It's way more clean to just check when the score value has changed.

One way to do this is like this.

Change the two variables to private:

 private int score, cs;

Then, add a new method like this:

 public void ChangeScore(int amount)
 {
   score += amount;
   if (score > cs)
   {
     PlayerPrefs.SetInt("arcadeHighscore", score);
     PlayerPrefs.Save();
    }
 }

You can then remove the Update method completely.

In the Score class, do this now instead of the old code:

 if (coll.CompareTag("Ball"))
 {
   scoreSys.UpdateScore(1);
 }

And as soon as you do that, you can actually merge the two classes.

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 Voyder_Rozann · Jul 15, 2017 at 06:35 AM

I think in void Start(), on the second script, you did put "score = 0" so every time it will set your highscore to 0 !

Comment
Add comment · Show 2 · 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 FlaSh-G · Jul 15, 2017 at 08:03 AM 0
Share

This is intended, as the score value is supposed to increased during gameplay.

avatar image Voyder_Rozann FlaSh-G · Jul 15, 2017 at 08:05 AM 0
Share

Oh... I didn't noticed that... Sorry !

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

138 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying to get my spawn manager to respond to the current score 1 Answer

Score and HighScore saving script [please help] 2 Answers

Score and Highscore - Highscore not saving, loading, or displaying. 0 Answers

How to add score after destroying object in unity 2D? 1 Answer

in this code whenever score is gretater than highscore then old highscore becomes zero and after the game ends it get updated 0 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