Save & Load System

So I made a clicker game and I want to save & laod the data from my game. But I cant find a way to save the status of my upgrade buttons (button.SetActive) I just cant. So if anyone can help me with that. I already have the other like the clicks and high score but not the buttons.

9183455–1279391–ClickerData.cs (660 Bytes)
9183455–1279394–SaveSystem.cs (1.14 KB)
9183455–1279397–Clicker.cs (3.09 KB)

Don’t save your buttons and don’t serialize any UI elements. Your UI should be separated from your game data and reflect and modify the game data.

Your game data might have like a configuration object that represents the configuration options the user has selected. This data object is the thing that you serialize and save, not the button. Your GUI should show the current value and update the value in its onclick handler.

2 Likes

^ ^ ^ ^ This.

  • Have data.
  • Allow input to modify data.
  • Have routines to output and use that data to make your game behave.
  • Save and load only the data.

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

Loading/Saving ScriptableObjects by a proxy identifier such as name:

https://discussions.unity.com/t/892140/8

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:

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.