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 ErinEB · Jan 02, 2018 at 02:46 PM · playerprefsguitextsave datascore systemhighscore

Setting, saving and getting info from PlayerPrefs, i can't save and call a highscore

I don't understand how to set my high score to save at the end of each game, i've looked at many things for high score with player prefs and have copied and pasted code but i don't think my script actually has a reference for wherever the data is supposed to be saved when i use PlayerPrefs. Do i need to make a game object to store or is it just supposed to know where to save? i'm really confused. here's everywhere i have reference to high score and PlayerPrefs in my script, what am i missing?

 public GUIText scoreText;
     public GUIText restartText;
     public GUIText gameOverText;
     public GUIText yourscoreText;
     public GUIText highscoreText;
 
     private bool gameOver;
     private bool restart;
     private int score;
     static int highscore;
 
     private void Start()
     {
         gameOver = false;
         restart = false;
         restartText.text = "";
         gameOverText.text = "";
         yourscoreText.text = "";
         highscoreText.text = "";
         score = 0;
         highscore = PlayerPrefs.GetInt("highscore", highscore);
         UpdateScore();
         StartCoroutine(SpawnWaves());
         
     }
 //skipping some lines
 
 public void AddScore(int newScoreValue)
     {
         score += newScoreValue;
         UpdateScore();
     }
 
     void UpdateScore()
     {
         scoreText.text = "Score: " + score;
     }
 
     public void GameOver()
     {
         gameOverText.text = "Game Over!";
         yourscoreText.text = "Your Score: " + score;
         gameOver = true;
         
 
         
 
 
         if (score > highscore)
         {
             highscoreText.text = "Highscore: " + score;
             PlayerPrefs.SetInt("highscore", highscore);
         }
 
         PlayerPrefs.Save();
     }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by OneCept-Games · Jan 02, 2018 at 02:52 PM

Are you calling GameOver() when your game is over?

Comment
Add comment · Show 3 · 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 ErinEB · Jan 03, 2018 at 02:26 PM 0
Share

yes i am, it's triggered when the player is destroyed by an object

avatar image OneCept-Games · Jan 03, 2018 at 02:46 PM 0
Share

You are updating a variable called "score" but you are saving another one called "highscore". "highscore" in your code is never updated with a new value, so you will always save and retrieve 0.

avatar image ErinEB OneCept-Games · Jan 03, 2018 at 03:31 PM 0
Share

thank you! i cant believe i missed that, it makes perfect sense. appreciate the help

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

74 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

Related Questions

using playerprefs to update a guitext and record highscores 1 Answer

Im Doing HighScore System To My Game And There Is Something Wrong In My Scripts 0 Answers

Highscore won't save 3 Answers

How to set a numerical getint() and display it... 1 Answer

Binary writer not saving game data on iOS builds 1 Answer


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