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 masterawai · Nov 07, 2014 at 09:14 AM · c#android

My high score do not update after restart

hello, i got a problem here. I'm developing shooting game. Then, i need to display the high score but it's doesn't shown. It's never update the score after restart. What should i do? I can't figure out the problem right now.

here my health script (attached to every enemy)

 using UnityEngine;
 
 public class HealthScript : MonoBehaviour
 {
 
     public static HealthScript instance;
     public int hp = 1;
     private GUIText scoreReference;
     private GUIText highscoreReference;
     private static int _highscore = -1;
     public int highscore { 
         get { if (_highscore == -1) 
             _highscore = PlayerPrefs.GetInt("Highscore", 0);
             return _highscore; 
         }
         set {
             if (value > _highscore) {
                 _highscore = value;
                 highscoreReference.text = _highscore.ToString();
                 PlayerPrefs.SetInt("Highscore", _highscore);
             }
         }
     }
 
     public bool isEnemy = true;
 
 
     private static int points;
     public void Damage(int damageCount) {
         hp -= damageCount;
 
         if (hp <= 0)
         {
             // Dead!
             Destroy(gameObject);
             points++;
             scoreReference.text = points.ToString();
         }
     }
 
     public void gameEnd() {
 
         points = highscore;
         points = 0;
     }
 
     //update from previous code
     void Start()
     {
 
     scoreReference = GameObject.Find("Score").guiText;
     highscoreReference = GameObject.Find("HighScore").guiText;
     scoreReference.text = points.ToString(); 
     highscoreReference.text = highscore.ToString ();
     instance = this;
 
 }

then this is my player script where i put the game over method

 void OnDestroy()
 {
     // Game Over.
     // Add the script to the parent because the current game
     // object is likely going to be destroyed immediately.
     transform.parent.gameObject.AddComponent ();
     HealthScript.instance.gameEnd ();
 }


UPDATE: THE PROBLEM SOLVED. JUST CHANGE THIS "points = highscore;" TO "highscore = points;". I DON'T KNOW BUT DOES IT MATTER TO BE LIKE THAT? I HOPE SOMEONE CAN EXPLAIN THAT..THANKS EVERYONE!

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
1

Answer by ashique · Nov 07, 2014 at 09:17 AM

You need to check the condition using PlayerPrefs only, u r using class variables to check the condtion those will be reset on restart...

Comment
Add comment · Show 12 · 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 masterawai · Nov 07, 2014 at 09:35 AM 0
Share

can you show how to do it?

avatar image ashique · Nov 07, 2014 at 09:41 AM 1
Share

if (_highscore == -1) _highscore = PlayerPrefs.GetInt("Highscore"); // GetInt dont take 2 //parameter

avatar image ashique · Nov 07, 2014 at 09:45 AM 1
Share

get {

return PlayerPrefs.GetInt("Highscore"); } set { if (value > PlayerPrefs.GetInt("Highscore")) { highscoreReference.text = _value.ToString(); PlayerPrefs.SetInt("Highscore", value); } }

avatar image masterawai · Nov 07, 2014 at 09:51 AM 0
Share

it still the same, the highscore do not update after restart...

 get { if (_highscore == -1) 
             _highscore = PlayerPrefs.GetInt("Highscore");
             return _highscore; 
avatar image ashique · Nov 07, 2014 at 09:54 AM 1
Share

get { highscoreReference.text = PlayerPrefs.GetInt("Highscore"); return PlayerPrefs.GetInt("Highscore"); } set { if (value > PlayerPrefs.GetInt("Highscore")) { highscoreReference.text = _value.ToString(); PlayerPrefs.SetInt("Highscore", value); } }

Show more comments
avatar image
0

Answer by ExtremePowers · Nov 07, 2014 at 11:33 AM

I think this would do it:

      public int highscore { 
          get { if (_highscore == -1) 
              _highscore = PlayerPrefs.GetInt("Highscore");
              return _highscore; 
          }
          set {
              if (value > _highscore) {
                  _highscore = value;
                  highscoreReference.text = _highscore.ToString();
                  PlayerPrefs.SetInt("Highscore", _highscore);
                  PlayerPrefs.Save();
              }
          }
      }
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 masterawai · Nov 07, 2014 at 12:01 PM 0
Share

hey thanks, it doesn't work, what wrong with this code?

 using UnityEngine;
 
 public class HealthScript : $$anonymous$$onoBehaviour
 {
 
     public static HealthScript instance;
     public int hp = 1;
     private GUIText scoreReference;
     private GUIText highscoreReference;
     private static int _highscore = -1;
     public int highscore { 
         get { if (_highscore == -1) 
             _highscore = PlayerPrefs.GetInt("Highscore");
             return _highscore; 
         }
         set {
             if (value > _highscore) {
                 _highscore = value;
                 highscoreReference.text = _highscore.ToString();
                 PlayerPrefs.SetInt("Highscore", _highscore);
                 PlayerPrefs.Save();
             }
         }
     }
     
     public bool isEnemy = true;
     
 
     private static int points;
     public void Damage(int damageCount) {
         hp -= damageCount;
         
         if (hp <= 0)
         {
             // Dead!
             Destroy(gameObject);
             points++;
             scoreReference.text = points.ToString(); 
 
 
         }
     }
     
     public void gameEnd() {
 
         points = highscore;
         points = 0;
     }
     
     void Start()
     {
 
         scoreReference = GameObject.Find("Score").guiText;
         highscoreReference = GameObject.Find("HighScore").guiText;
         scoreReference.text = points.ToString(); 
         highscoreReference.text = highscore.ToString ();
         instance = this;
 
         
     }

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

cant delay with WaitForSeconds C# 2 Answers

Distribute terrain in zones 3 Answers

"using Android" doesn't recognizable on Unity 1 Answer

Android Development; How to use SDK manager! 1 Answer

Anisprite animation from OnTriggerEnter 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