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 PilgrimFlowers · Apr 27, 2021 at 11:36 PM · playerprefsupgradeupgradeshealth

PlayerPrefs resetting values on startup/restart?

Hi there. In my game I am using PlayerPrefs to store a currently available max health because I have health upgrades so I want the player to be able to keep those health upgrades each playthrough. The player will by default start with 3 "hearts" and then as they get upgrades, I am setting the "startHearts" value in playerprefs to be the upgraded amount, then retrieving that value on start up. But for some reason it keeps going back to "3" on start up even though I'm not setting it anywhere. It seems to favour the "Inspector's" value over the PlayerPrefs, because when I change the value in the inspector, it always starts with whatever I have in the inspector.

Here is my character tracker which checks for and sets the values on start up:

     void Start()
     {
         if (PlayerPrefs.HasKey("UnlockedStartHearts"))
         {
             startHearts = PlayerPrefs.GetInt("UnlockedStartHearts");
             currentHealth = PlayerPrefs.GetInt("UnlockedStartHearts") * 4;
             currentMaxHealth = PlayerPrefs.GetInt("UnlockedStartHearts") * 4;
         }
         else
         {
             startHearts = 3;
             currentHealth = 12;
             currentMaxHealth = 12;
         }
     }

And here is my Player Health Controller which checks for then sets the values from the Character Tracker:

     void Start()
     {
         currentMaxHealth = CharacterTracker.instance.currentMaxHealth;
         currentHealth = CharacterTracker.instance.currentHealth;
         startHearts = CharacterTracker.instance.startHearts;
         PlayerPrefs.SetInt("UnlockedStartHearts", startHearts);
         PlayerPrefs.Save();
 
         Debug.Log("PPrefs 'start' StartHearts set:" + PlayerPrefs.GetInt("UnlockedStartHearts").ToString());
 
         maxHealth = maxHearts * healthPerHeart;
         CheckHealthAmount();
     }

The PlayerHealthController accurately keeps the "start"hearts" and upgrades between levels, but then if I got back to the main menu and start a new game, it starts back at 3 (the value in the inspector) instead of the saved upgraded amount.

Is this only an issue while using the editor, and upon a build will work fine (I have not tried building and testing yet) because I know PlayerPrefs stores the values differently between the editor and the actual built game?

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 PilgrimFlowers · Apr 28, 2021 at 12:01 AM 0
Share

Confirmed; it does only do these when playing within the editor. A built version acts properly... So I don't know what to make of that. Haha.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by PilgrimFlowers · Apr 27, 2021 at 11:47 PM

Also, here's the script for adding an upgrade, in case that also helps:

     public void AddHeartContainer()
     {
         startHearts++;
         startHearts = Mathf.Clamp(startHearts, 0, maxHearts);
 
         currentHealth = startHearts * healthPerHeart;
         currentMaxHealth = startHearts * healthPerHeart;
         PlayerPrefs.SetInt("UnlockedStartHearts", startHearts);
         PlayerPrefs.Save();
         CharacterTracker.instance.startHearts = startHearts;
 
         Debug.Log("PPrefs 'addheart' StartHearts set:" + PlayerPrefs.GetInt("UnlockedStartHearts").ToString());
 
         CheckHealthAmount();
     }
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

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

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

Help Saving Max Health With PlayerPrefs 1 Answer

Deleting PlayerPrefs is killing my player 2 Answers

Where do I put PlayerPrefs.SetInt? 1 Answer

ExtensionOfNativeClass attribute missing error in Unity 2018.3 8 Answers

Health bar depletes when app isn't open 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