• 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 savantedroid · Oct 10, 2020 at 08:54 AM · playerprefsbug-perhaps

PlayerPrefs Int value becomes 0 after update

Hello,

I believe this is a bug. My game plays OK normally, I terminate the app and restart multiple times and it keeps the PlayerPrefs info well. But when I update the app with a newer version, the level value goes 0. Is there a reason why playerPrefs may be getting zeroed? I am using Unity 2020.1.7f1

 private void Start() {
         if (PlayerPrefs.HasKey(LEVEL)) {
             print("Has Key " + PlayerPrefs.GetInt(LEVEL));
         } else {
             print("No Level Key");
         }
         level = PlayerPrefs.GetInt(LEVEL, 1);
         Debug.Log("Level " + level);
 
         ToggleMenu(MenuSelect.start);
     }

And the XCode says:

 (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)
 
 Has Key 0
 UIManager:Start()
  
 (Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)
 
 Level 0
 UIManager:Start()


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

Answer by SuperMalvado592yt · Oct 10, 2020 at 11:35 AM

Hi, I'm not very good at unity, I am quite new in this program, but I think I can help you a bit. I don't know how playerPrefs worked before, but now I think you should add these little commas from above (" ") with the name of the player pref. Also, in the debug.Log you wrote other name for the player prefs. You use ("LEVEL") and in the debug.log you wrote ("Level") so I think unity recognize them as different playerprefs, giving you the default value (0). Something like this:

private void Start() { if (PlayerPrefs.HasKey("LEVEL")) { print("Has Key " + PlayerPrefs.GetInt("LEVEL")); } else { print("No Level Key"); } level = PlayerPrefs.GetInt("LEVEL", 1); Debug.Log("LEVEL " + level);

      ToggleMenu(MenuSelect.start);
  }

The thing of the commas (" ") I meant to write:

PlayerPrefs.GetInt( "LEVEL", 1); //for example.

And as I told you, the debug.log has a different player prefs.

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 Bunny83 · Oct 10, 2020 at 11:40 AM 0
Share

LEVEL (written in all caps) most likely is a string constant as it's common to use constants here to avoid spelling mistakes. So he most likely has a

 const string LEVEL = "TheActualLevelKeyString";

somewhere. By using the constant you can ensure you don't mess up the string when used multiple times. If LEVEL wasn't defined the code would not compile and if it doesn't compile it can not run.

avatar image savantedroid Bunny83 · Oct 10, 2020 at 11:41 AM 0
Share

That is correct!

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

145 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

Related Questions

Where on iPhone editor are playerprefs stored. 1 Answer

PlayerPrefs.DeleteAll not working 1 Answer

PlayerPrefs saving 1 Answer

Guide in ArrayPrefs or PlayerPrefx? 1 Answer

Are PlayerPrefs the same on iPhone compared to my computer? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges