- Home /
PlayerPrefs are deleted after compilation
I'm working on a simple 2D android game now. I use playerprefs to store all app data.
After another compilation of the project, I launched it on my android device and saw that a record text field had "-1" value. (explanation: I have "-1" value in the text field by default. When I run the game, it updates according to the playerprefs key value. If it is a first launch, it has "FIRST_LAUNCH". In other cases this key has name of a previous scene. "-1" means that this text hasn't been updated because my key don't even exist!)
void Awake() {
switch(PlayerPrefs.GetString("PreviousScene")) {
case "FIRST_LAUNCH":
case "challengeHub":
case "shop": record_text.text = record.ToString(); break;
case "main":
case "game": InvokeRepeating("recordUpdate", 0, 0.02f); break;
default:
Debug.LogWarning("It's never gonna happen.");
break;
}
}
I always reset all playerprefs (DELETE all of them, SET default values and SAVE) before each compilation in order to have parameters I want to be for the first app launch.
Ep. "PreviousScene" key has "game" value -> PlayerPrefs.DeleteAll();
-> PlayerPrefs.SetString("PreviousScene", "FIRST_LAUNCH");
-> PlayerPrefs.Save();
So as I said before, it displays "-1" that means playerprefs no longer save its keys/values after compilation(!) of the project. BUT if I hit "Play" in the editor after reset, it behaves itself like I want to ("-1" turns to "0" and other stuff. And also I see the message in console about the first launch).
And I don't know what to do now! It had always worked! It had been a way I always used to set the def. parameters. I want to note that the issue appeared suddenly with no reason.
I've created an absolutely new project and do nothing with compilation settings except product name, company name and package name, added to PP some int key with value of 50 and connect it to the text field.
PlayerPrefs.SetInt("testValue", 50);
I've deleted this line of code with pp.set to prevent setting the value each time I enter the app. So when I hit "play" in the editor, it sets text field to "50" ( I have "-1" by default). BUT when I run this app on my android device for the first time, I see "0", which means that app can't find my key with value "50" and sets ITS default value for int parameter - "0".
public Text testText;
private void Start() {
testText.text = PlayerPrefs.GetInt("testValue").ToString();
}
I reinstalled unity and all android sdk's but the issue is still there. Is there anyone who can help me or give an advise please? I would be very grateful.
PlayerPrefs provide a serialization/deserialization solution hiding the details and keeping a consistent target location for each platform. If you read the documentaion you ll notice the location is not inside game folder or project folder (if you work in editor), thus that data are not included in the build (well its not for that exact reason but for simplicity lets stick with it).
Think of it this way, say I have game installed in my pc and I save, lets assume game files exist on C:\program files\my game and the save file is in C:/users/.../appData/myGame/Saves. If i send you the Game (the game files) you ll be able to play it on your machine but you won't have the save file i created, unless explicitly include the save file i created and placed exactly in the same path as the game would expect it to be. On the other hand if I delete the game in my pc and reinstall it (with out deleting the save file previously created) I ll be able to use it again (that happens when you work in the editor, pretty much, once again simplified).
To conclude playerprefs are not meant to used the way you intent, for default values the easiest way is to directly set the values in the editor, while playerprefs are more suitable for saving data between game sessions.
Your answer
Follow this Question
Related Questions
Same Playerprefs for 2 games? [Android] 1 Answer
Playerprefs not working on android 0 Answers
PlayerPrefs in android 2 Answers
Double icon in Android app drawer 1 Answer
PlayerPrefs file wrong stored location 0 Answers