Hello,
I’m trying to make a Beta version of my game to send out to testers now, but I’m finding that whenever I make a Windows build, my PlayerPrefs seem to be carrying over from my old builds (despite me deleting them). I also could’ve sworn this wasn’t happening before with my earlier builds, which always started from scratch. Is is something to do with me now using Unity 2021?
This concerns me because if I send these Betas out, will that mean they’ll automatically have all the unlockables that were in my version?
I’ve read something about making a script for deleting all PlayerPrefs when the game is booted, but I use A LOT of PlayerPrefs, and the essential ones (score, health, etc) are already reset when a new game is started. Other things, such as high scores, option settings and unlockables, I want to be saved when the player exits the game, and still be there when they boot it again.
Is there a reason this has suddenly started happening? I had always thought a new build = PlayerPrefs resetting to 0, and it had seemed this way with my earlier builds too until today.
I hope someone can help clear this up for me, whether or not it’s a version issue, and offer a more sound solution.
Thanks in advance.
You might want to consider just packing them up in your own custom structure (or dictionary) and serializing them out with JSON to a single textfile.
The main reason is Unity stores PlayerPrefs in the Windows Registry (why?!) and barfing tons of stuff into the common system hive isn’t a great idea.
If you make your own PlayerPrefs equivalent that just reads/writes from a string-indexed dictionary and replace all PlayerPrefs calls with that, it would be a pretty easy engineering lift.
The point of that is you can trivially version it at the save/load point, by any number of ways:
Also, keep this in mind for JSON happiness:
Problems with Unity “tiny lite” built-in JSON:
In general I highly suggest staying away from Unity’s JSON “tiny lite” package. It’s really not very capable at all and will silently fail on very common data structures, such as Dictionaries and Hashes and ALL properties.
Instead grab Newtonsoft JSON .NET off the asset store for free, or else install it from the Unity Package Manager (Window → Package Manager).
https://forum.unity.com/threads/jso…-not-working-as-expected.722783/#post-4824743
https://assetstore.unity.com/packages/tools/input-management/json-net-for-unity-11347
Also, always be sure to leverage sites like:
https://jsonlint.com
https://json2csharp.com
https://csharp2json.io
PS: for folks howling about how NewtonSoft JSON .NET will “add too much size” to your game, JSON .NET is like 307k in size, and it has the important advantage that it actually works the way you expect a JSON serializer to work in the year 2021.
Followup: as I suspected, this work has already been done many times:
A replacement for Unity’s PlayerPrefs that stores data in a JSON file.
It’s kinda nice and allows you to have multiple instances of a “PlayerPrefs” thing but you could always hack it up to just be pure static like the single true PlayerPrefs.
I’d been avoiding JSONs because last time I tried to use one (following a tutorial on making a scoreboard) I kept getting tons of errors, despite following what the tutorial did.
Regardless, would this actually fix the build issues of carrying over my saved data into new builds?
On another note, is this a version thing? Because I’m pretty sure when I was using Unity 2020, my PC builds weren’t carrying over the PlayerPrefs from my Unity Editor test runs (actually, whenever I made a build before, the PlayerPrefs in Unity’s editor would actually reset too).
No, but it would ENABLE you to do so by putting a version in there and having it all in one spot.
Come to think of it, there’s nothing preventing you from doing this right now:
Make a PlayerPref called “AppVersion” and stick a number in there.
Make sure that number changes with every build (look up how to do make that automatic).
On startup, if the number does not match, do a PlayerPrefs.DeleteAll() and start afresh.
But really, barfing data all over the Windows hive is … sub-optimal app behavior. 