Don't Destroy on Load Object and GameData

Hello,

I want to save my game data as the game progresses using a Don’t Destroy on Load object.

I have:

  • A GameData class/script which stores values like health, magicType, etc.
  • A SaveLoad class/script which saves current values in the GameData class to Json
  • An empty game object called PlayerData that has both of the above scripts

Currently, I’m saving values in one scene by using button click events. My object attached to the button is the PlayerData object, and I change values using the GameData script on the click event.

But this only works on the first scene - I can’t attach the PlayerData object and store its GameData values because there is no PlayerData object in the editor.

So how can I attach this object, which is in a different scene? Or am I doing this wrong?

Many thanks

you will need to add listeners at runtime for each button if you need to use the dont destroy on load, something like this

theButton.onClick.AddListener(yourPersistentObject.YourMethod);