Player Customisation: Save & Load

Hi everyone,

I’ve built a player customisation feature, which currently works. However, I’m unsure as to how to save/load the customisation so it’s still there when I close and re-open the game.

I’m fairly new to Unity, been self-teach myself over a number of months now and tryed to avoid asking for help as much as possible, but I am at a real block here.

I’ve attached my sections of code that relate to applying new skin and hats to my player which works (appart from the skin not carrying over to the next scene (but hoping solving the saving/loading thing will fix this as well).

I’ve created Int PlayerPrefs to save the Index number the customisation items but have be unable to get it to remember what was chosen before when reloaing the game no matter what I try.

Any and all help would be very much appreciated.

Many thanks.

7942843--1016164--Customisation.png
7942843--1016167--PLRMovement.png

I already moved your post on this subject to this forum.

Note that I also commented on the subject too but you didn’t reply about that part.

This is certainly a way to do it, and it certainly works, so you must track down why your use of PlayerPrefs is not giving you the consistent results you want. You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

1 Like

The previous post wasn’t gett any other responses so I figure best to re-post which hopefully wouldgive more clarity. Appologies if this has cause any issues.

I’ve been attempting the Save function you suggested, however I’ve been unsuccessfull in utalising it correctly with the code that i’ve used.

@Kurt-Dekker Thank you for your seponce, the exisiting code through Dubugging all seems to be working fine. I’m requiring a missing peice to this puzzel and unsure as how to load these preferences set in the previous run on start.

So far I have found the Index value resets itself to 0 on quiting the game (regardless of the PlayerPrefs value) and the sprite library resets to the player prefab’s original sprite library.

If either of you have any other advice or options you know of that could help, it would be very much appreciate.

Many thanks.

Load/Save steps:

https://discussions.unity.com/t/799896/4

An excellent discussion of loading/saving in Unity3D by Xarbrough:

https://discussions.unity.com/t/870022/6

When loading, you can never re-create a MonoBehaviour or ScriptableObject instance directly from JSON. The reason is they are hybrid C# and native engine objects, and when the JSON package calls new to make one, it cannot make the native engine portion of the object.

Instead you must first create the MonoBehaviour using AddComponent() on a GameObject instance, or use ScriptableObject.CreateInstance() to make your SO, then use the appropriate JSON “populate object” call to fill in its public fields.

If you want to use PlayerPrefs to save your game, it’s always better to use a JSON-based wrapper such as this one I forked from a fellow named Brett M Johnson on github:

https://gist.github.com/kurtdekker/7db0500da01c3eb2a7ac8040198ce7f6

Do not use the binary formatter/serializer: it is insecure, it cannot be made secure, and it makes debugging very difficult, plus it actually will NOT prevent people from modifying your save data on their computers.

https://docs.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide