Many game allows the player to save progress at any time and load the save back at any time (like Minecraft and Doom 3).

I would like to implement it in Unity.

I think I will need to put the value of every variable in every script into a save file, but how? Or maybe not?

  • \$\begingroup\$ Not necessarily every variable. You probably have some things that are important to preserve exactly (eg. the player's health, contents of their inventory, what level they're in...) and some things you can discard (exact progress through a UI animation or background music). Once you've identified what data you want to save, you package it up into a data structure and save that structure to the Application.persistentDataPath using ordinary file IO. You load it the same way. What specific obstacle have you encountered in implementing this? \$\endgroup\$ – DMGregory Jul 30 '17 at 16:53

Unity doesn't offer a savegame system out-of-the-box. You won't get around writing and reading files yourself using the usual file access tools the .NET standard library offers (check stackoverflow.com about how to read and write files in C#. That topic got asked and answered to death by now).

But Unity comes with one class which can make your life a lot easier: JsonUtility. This class allows you to convert objects to JSON strings and back. You can only use it on classes which have the Serializable attribute.

Note that it will only serialize/deserialize public variables and attributes.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.