• 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 aidangig56 · Oct 10, 2014 at 11:12 AM · playerprefssavedatahighscore

PlayerPrefs save after quit?

I am trying to save highscores for my motorcycle game. But when I close the app out of my iphone and reopen it once I score some points it overrites the highscore.This is only after I close the app completely. I am trying to display the player prefs after I get the value with a guitext. function Start () {

         highscoretext.text = (PlayerPrefs.GetInt("Highscore")).ToString ();
 
 
 function Update()
     {
         PlayerPrefs.Save();
         if(score > high_score) {
                     high_score += 100;
                     highscoretext.text = "HIGHSCORE : " + high_score;
                     PlayerPrefs.SetInt("Highscore", high_score);
                 }
Comment
Add comment · Show 4
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 Turkeybag · Oct 10, 2014 at 11:22 AM 0
Share

Would you be able to post the full script(s)? I can't really get a good idea form just that. Also I would avoid putting PlayerPrefs.Save() in the update function as that will cause performance problems.

avatar image Eric5h5 · Oct 10, 2014 at 11:24 AM 0
Share

PlayerPrefs should only be used where actually necessary, not every frame in Update.

avatar image Josh707 · Oct 10, 2014 at 12:09 PM 0
Share

Do you ever get a value for high_score from PlayerPrefs? From what you've posted you're only setting the GUIText's text in Start(), not the high score itself.

avatar image aidangig56 · Oct 11, 2014 at 01:15 PM 0
Share

Yes I get an int value from the high_score its a variable.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by fafase · Oct 11, 2014 at 02:19 PM

I would say you do not really need to save in the Update. Since you are on iOS you can use:

http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationPause.html

or:

http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationQuit.html

but make sure you read the bottom line of the page about the iOS not quitting.

Then you are getting the value on Start and again you do not need to check in Update, create a method to update the score value and at the same time check if bigger than the HiScore:

 private var highScore:int;
 private var score:int;
 private var maxValue:int = 10000000;
 
 function Start()
 {
    highScore = PlayerPrefs.GetInt("highscore");
    highscoretext.text = highScore.ToString(); 
 }
 
 function AddScore(newScore:int)
 {
     if(newScore < 0)
     {
         Debug.LogError("Negative value?...Really?");
         return;
     }else if (newScore > maxScore)
     {
         Debug.LogError("That is a big score??!!");
         return;
     }
     score += newScore;
     if(score > highScore)
     {
         highScore = score; 
         highscoretext.text = highScore.ToString(); 
     }
 }
 
 function OnApplicationPause(pauseStatus: boolean) // Or Quit
 {
     PlayerPrefs.SetInt("Highscore", high_score);
     PlayerPrefs.Save();
 }



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 767_2 · Oct 11, 2014 at 01:59 PM

grab the save and set out of update and put it in your quit(exit) button

  function exitFromGame()
       {
        if(currentScore>high_score)
         PlayerPrefs.SetInt("Highscore", currentScore);
        PlayerPrefs.Save();
      }


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 aidangig56 · Oct 11, 2014 at 02:03 PM 0
Share

Whenever the game is closed and reopened and I collect point from int vars it still overwrites the old highscore regardless of the score is higher than the highscore.

avatar image 767_2 · Oct 11, 2014 at 02:11 PM 0
Share

so you can check if your score is more than the highscore , i edited

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to save a high score 2 Answers

Saving highscore with PlayerPrefs 1 Answer

is it safe to save info in PlayerPrefs? 2 Answers

Keeping Highest score - Star Rating System 3 Answers

Alternative for PlayerPrefs 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