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 /
  • Help Room /
avatar image
0
Question by AustinDrozin · Jun 24, 2016 at 03:34 AM · c#playerprefscurrency

PlayerPrefs not saving after restarting level in editor

Hi,

For my skeet shooting game, all I want to do is keep track of the amount of coins the player has. So I set up a simple script that handles all the player's coins. Here it is:

 private int pullMethod;
     private int playerCoins;
 
     void OnEnable()
     { 
         EndlessDestroyer.moneyDiscDestroyed += MoneyDiscDestroyed;
     }
 
     void OnDisable()
     {
         EndlessDestroyer.moneyDiscDestroyed -= MoneyDiscDestroyed;
     }
 
     void LevelWonCoinEarnings()
     {
         playerCoins = playerCoins + 5; //adds coins after beating a level
         SaveCoins();
     }
 
     void MoneyDiscDestroyed()
     {
         playerCoins = playerCoins + 10; //adds coins after destroying a money disc in endless
         SaveCoins();
     }
 
     void SaveCoins()
     {
         PlayerPrefs.SetInt ("PlayerTotalCoins", playerCoins); 
         PlayerPrefs.Save();
         print(PlayerPrefs.GetInt("PlayerTotalCoins"));
     }

In the console I see the coins adding up as I shoot down the correct discs in the air, but whenever I restart the level, nothing saves. The next time I shoot down a disc, the total player coins have just reset back to 0.

I for the life of my cannot figure out why this isn't working. Like If I am being stupid, and the error in my code is staring me right in the face, then please tell me haha. Help!

Thank you

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
1
Best Answer

Answer by corn · Jun 24, 2016 at 04:12 PM

You correctly save the data, but you never retrieve it. When you restart the level, playerCoins is equal to 0. The correct data is stored in PlayerPrefs, but you need to explicitly assign it to playerCoins, otherwise the script is going to start with 0, increment it as you shoot discs, and erase the old data.

 void OnEnable()
 {
     playerCoins = PlayerPrefs.GetInt("PlayerTotalCoins");
     EndlessDestroyer.moneyDiscDestroyed += MoneyDiscDestroyed;
 }
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 TBruce · Jun 24, 2016 at 04:47 PM 1
Share

@corn is correct but pass a default value to it otherwise the first time the game is run it will throw an error because the key will not yet exist. Like this

 playerCoins = PlayerPrefs.GetInt("PlayerTotalCoins", 0);
avatar image AustinDrozin · Jun 24, 2016 at 07:12 PM 0
Share

Thank you so much! This worked. I wasn't aware of how this truly worked I guess.

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

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

I'm having a problem while jumping an object in Unity 5 0 Answers

PlayerPrefs returns nothing 0 Answers

Save player rotation in playerprefs c# 2 Answers

need help with code please 1 Answer

Playerpref Saving Lag 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