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 /
avatar image
0
Question by CreativeTrager · Jan 11, 2019 at 09:27 AM · androidplayerprefscompilation

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.

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 PizzaPie · Jan 11, 2019 at 11:52 AM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

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

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

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


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