Playerprefs not working with Andriod

I have tried to save using player prefs in the Andriod app, but it does not save on the phone, anyone able to do this with the andriod and Unity?

I ported my iPhone app over, and did nothing to any of my playerprefs code and its all working just fine on the Nexus One.

PlayerPrefs are working fine but PlayerPrefs.DeleteAll is not. We have just worked around that one.

In my code, I am writing to the playerprefs and can load and use it only while the game is running, when I quit the game the subsequently reload it, the playerprefs is empty and I can’t retrieve the last saved game data. So basically what I have right now, is

    void savePrefs()
    {
        PlayerPrefs.SetString("data", _data);
    }

    void loadPrefs()
    {
        PlayerPrefs.GetString("data");
        prepareGameItems();
    }

_data is defined as a string and it does save and load in, but as I mentioned, it will only do it for the active game and wile the game is loaded, if I quit the game, the PlayerPrefs value for data is empty…

Is it possible for me to post my compiled package for someone else to test on their Android?

I have taken the save/load and added it to my XMLDemo code, I can post the package if anyone can simply deploy it up to their Andriod phone and test it, once loaded, simply use the “Build Blocks” then use the “Save Prefs” button, a new button will show up that allows you to use the saved values for setting them “Set Loaded Gameobjects”, the thing is, since this exists in the scene, that means that the prefs did save. The way to check this, is to click that button, then touch the “clear blocks” button at the top, then touch the “load prefs” and the blocks will load up and the “Set Loaded Gameobjects” will appear again which states that the load worked. Once you have done this, simply quit using the “Quit” button, then go back in the game and click on “Load Prefs” and nothing will load. … …

I really need this confirmed by someone else besides myself so that I know that I am not going mad.

400209–13744–$XMLDEMOProject.unitypackage (49.4 KB)

In the code you included there, the return value of GetString isn’t being assigned to anything. Did you mean for it to be assigned back to that same “data” variable?

Yes… good catch on the example, I have that in my game code but not what I just converted, that section of code should read:

    void savePrefs()
    {
        prepData();
        PlayerPrefs.SetString("data", _data);
    }

    void loadPrefs()
    {
        _data = PlayerPrefs.GetString("data");
        prepareGameItems();
    }

I need to stop rushing to get examples of problems ready.
Another question about this, when you update your game and push it out, how is the PlayerPrefs value kept without purging when an update is pushed to the phone since technically I think this “secured area” for the app is purged and rebuilt from an update? So once I do get this going correctly, what is stopping my saved games from being lost on the persons phone when I push out a new build of the same application?

It is possible to uninstall/reinstall an app while still keeping stuff like player prefs, cache etc. This is actually what Unity does when you select Build&Run, and I assume this is how the PackageManager handles an updated version from the Android Market too. The only caveat is that the updated version needs to be signed with the same key, presumably because this key is somehow involved in managing the application specific files.

The key that Unity generates is good enough for this purpose right?

Whenever I load a new build on my NexusOne, the playerPrefs are always cleared - even when using the same key in the publishing settings. I also pushed an update to Marketplace and am getting support emails from players saying their progress has been cleared. Any tips on what might be going wrong?

Any ideas erique? I’d like to get this fixed for the next update as we are getting negative comments in Google Marketplace about the update erasing player’s progress.

When doing build&run from the editor and you have keysigning enabled (ie not debug key) the editor will do a clean install (this is to prevent trying to update binaries signed with different keys (that doesn’t work). But if you create two .apks (ie version 1.0 and version 1.1) and install them after each other manually the playerprefs should be kept. If they don’t please report that bug. We are not actively reseting the playerprefs, so either something changed in the update mechanism, or it’s something else entirely.

Thanks erique I’ll give that a go and let you know what happens!

Update:

Using build run in the editor with the debug key does keep the PlayerPrefs (as you predicted). I then created two apks (using the same non-debug keys) v 1.0 and v 1.0.1. I was able to load the 1.0 version onto the NexusOne using adb, but it wouldn’t let me replace it with the new apk, e.g. in Terminal:

SyBookPro:~ simonedis$ adb install Documents/Android/FinalApps/CrazySnowboard/Marketplace\ 1.0/Snowboard.apk
918 KB/s (20930376 bytes in 22.256s)
	pkg: /data/local/tmp/Snowboard.apk
Success
SyBookPro:~ simonedis$ adb install Documents/Android/FinalApps/CrazySnowboard/Marketplace\ 1.0.1/Snowboard.apk
1246 KB/s (21194695 bytes in 16.602s)
	pkg: /data/local/tmp/Snowboard.apk
Failure [INSTALL_FAILED_ALREADY_EXISTS]

…so I can’t really check to see if it is saving the PlayerPrefs. Is there another way to install the update?

Update 2:

I found the ‘adb install -r’ command which installed the update successfully.

…and more importantly I found my error, which was the PlayerPref save name had been changed in my code - so the good news is that Unity PlayerPrefs is working perfectly for updates. My bad! - thanks for your patience and help!

I have run across a problem on my galaxy s that involves PlayerPref never saving anything.
It’s still very hazy what triggers it, as I’ve tried a lot of stuff.

Works on other phones, and used to work on my galaxy s too, even with the version that is now bugging.

Weird.

are you on android 2.1 or 2.2?

On Android 2.2 … Sorry about so slow reply. I must turn on some email-notifications so I won’t post&forget.

I made a custom PlayerPrefs class to allow faster saving on Android.
As it uses a different way of saving data, it would probably be a good work-around for your issue.

1 Like

I seem to have a weird bug, which ‘sometimes’ on ‘some devices’ leads to PlayerPrefs not saving anything. has anyone else experienced this or has any idea how to fix that bug? The really bad thing about the bug is that although it seems to appear regularly (approx. 1% of our users) I haven’t been able to reproduce it once with any of our devices, so Im stabbing in the dark here. For starters I would be interested in what circumstances would prevent the PlayerPrefs from saving anything? Would insufficient space would lead to data loss? Could it be a permission thing? I have no idea. One user though reported it went away after reintstalling, but that doesn’t always seem to help. Also I had users reporting it stopped working after the last update, but then again it still works for the majority (99%) of the users.

Tried that custom PlayerPref class of yours and like the regular one, it’s not saving anything to my Android phone. By now I’m really desperate to get the prefs to work, because without it I can’t release my game because I’m linking the Android prefs to an online database.

@ , I’m guessing there must be something specific in your project because we don’t have any bug reports regarding player prefs that hasn’t been solved. Please file a bug report and attach a repro project so that we can have a look at what’s going wrong.

I have figured it out. There was totally nothing wrong with the PlayerPrefs class at all. The problem came from an other part in the code that I figured out now. I noticed that in the database the DateTime stamp stayed the same with each new PlayerPrefs made on the devide, so I figured something was stored in PlayerPrefs but an other thing went wrong. After an hour of trial & error I found out that the WWW.Progress would never reach a full 1 (completed, which I checked in the code) on the Android device. Only checking if WWW.progress!=null was enough to get the whole thing running on my phone! Now it’s time to get into some further testing on my phone.