• 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 calle_brown · Aug 26, 2019 at 07:48 PM · playerprefssavesaving

PlayerPrefs not saving after i quiet application

Hi! so i have a playerpreffs thats not saving after i quiet the application and one that does, the code of them look the same (to me), and wondering why its not saving

{

 private GameObject StartPos;
 private GameObject PlayerPos;

 public Text scoreText;
 public Text hiScoreText;

 public static float scoreCount;
 public static float hiScoreCount;
 private float yDistance;

 public static float statScore;
 public static bool justDied = false;
 public static float NewStatScore;
 

 public float pointsPerSecond;

 public bool scoreIncreasing;



 private void Start()
 {
     StartPos = GameObject.FindGameObjectWithTag("StartPos");
     PlayerPos = GameObject.FindGameObjectWithTag("Player");

     if (PlayerPrefs.HasKey("HighScore"))
     {
         hiScoreCount = PlayerPrefs.GetFloat("HighScore");
     }

     if (PlayerPrefs.HasKey("TotalHeight"))
     {
         NewStatScore = PlayerPrefs.GetFloat("TotalHeight");
     }
 }

 
 private void Update()
 {
     if (scoreIncreasing && playerController.dead == false)
     {
         // points per second
         yDistance = PlayerPos.transform.position.y - StartPos.transform.position.y;
         scoreCount = pointsPerSecond * yDistance;
     }
     
     if (scoreCount > hiScoreCount)
     {
         hiScoreCount = scoreCount;
         PlayerPrefs.SetFloat("HighScore", hiScoreCount);
         
     }
     // print out on screen
     scoreText.text = "Score: " + Mathf.Round(scoreCount) + "m";
     hiScoreText.text = "High Score: " + Mathf.Round(hiScoreCount) + "m";

     if (justDied == true)
     {
         NewStatScore = NewStatScore + scoreCount;
         PlayerPrefs.SetFloat("TotalHeight", NewStatScore);
         justDied = false;
     }

 }

}

here i have 2 player preffs. one thats TotalHeight and one thats Highscore. The highscore one is saving while the TotalHeight isnt. any tips?

Comment
Add comment · Show 1
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 SirPaddow · Aug 27, 2019 at 09:38 AM 0
Share

New attempt. It must be unrelated to PlayerPrefs. Could you show us where you set "justDied" to true ?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SirPaddow · Aug 26, 2019 at 08:01 PM

Are you sure the highScore is saved after you quit? You have to call this function to actually save the PlayerPrefs between sessions :

 PlayerPrefs.Save();
Comment
Add comment · Show 6 · 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 calle_brown · Aug 26, 2019 at 08:35 PM 0
Share

do i need a PlayerPrefs.Save(); for both of the prefs? or where would i put that in, and yes weirdly enough my highscore saves through sessions

avatar image SirPaddow calle_brown · Aug 26, 2019 at 08:43 PM 0
Share

Any time you call this function, it should save all the playerprefs that you set before, during this session.

avatar image calle_brown SirPaddow · Aug 26, 2019 at 09:25 PM 0
Share

Okay thank you!

Show more comments

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

121 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

Related Questions

PlayerPrefs.DeleteAll() not deleting unless app restarts 0 Answers

Saving and loading game with PlayerPrefs 1 Answer

Adding Value to Already Saved Values 1 Answer

Unity problem when i convert playerprefs save to the standard file io save 0 Answers

Saving Data! PLEASE HELP 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