- Home /
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()
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.
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.