How do i save data in Unity?

Hi guys, so currently i’m a beginner at game developing, but ik the basics. So i wanted to make a SaveGame function for my game, i’ve seen some videos online on how to do it, but i wanted something different… In those videos they were saving one or two data probably about the player’s position and the HP, but I wanted to save lotta data, like imagine they are aliens in the game, when the player saves the game, all of the aliens will be saved, the guns will be saved, the position, time and Animations, everything at once… And I don’t know how to keep track of everything, like the animations, wich exact frame was the animation in? Yeah, basically those stuff… Some of you may heard of Serious sam game, in that game the saved data contains everything including animations and… But that was serious engine and not unity… Isn’t there a package or anyway to save the whole scene at once?
Can anybody help me? I hope i explained it well.

There are tools that help with saving on the Asset Store. But “save the whole scene” is not a valid approach for creating a savegame - first, a scene isn’t “saveable” in the runtime, only in the editor, and then it contains a whole lot more than what is necessary to restore the game state.

Also, Unity has its own Serialization package but this is targeting experienced programmers (hence I won’t link it).

Animations are practically never saved in a savegame. For instance if there is a door open animation that takes 3s and the player saves the game in the middle of the animation, then loads the game, the game will typically show the door in its final state (open or closed).

Only when the animation is VERY important to gameplay they get saved, but not the animation itself, just the timestamp to restore the animation to somewhere between its start and end pose.

If you don’t know how to keep track of things that’s exactly what you need to be looking into. Every enemy I suppose will be a prefab instance spawned to some location. If there is a common parent “Enemies” then you can use that to go over each enemy. You could also store their game object references in a custom list, but also remove them when they “die”.

It’s doable. i assume you mean to implement it similar to the way an rpg like skyrim does. But it’s rather time consuming. It’s a good project for beginners though, youl learn alot of other fundamentals, theres tonnes of documentation on each part and it can be reused for future projects and potentially published as an asset if it’s good enough.

Conceptually you would first need a script holding a list of all objects that need to be saved. As an object is destroyed (eg. when a character dies) you can remove it from the list.
On each character would be another script that saves there location, inventory and other relavnt info (like name and hp).

When the save button is clicked, the master script with the list of objects would call the “save” function in all the scripts attached to the characters.

The save function in the characters scripts would save their data to a text file.
The load function triggered on scene load would do the opposite, loading the data from the text file.

Using a simple text format makes it easy to debug and manipulate. all you need to do is open the created text files to check if it worked correctly. But that means players could easily edit their saved games, by editing these files (which honestly you could easily sell as a feature).

To avoid manually adding a saveCharacter script to every individual npc, make use of prefabs.

theres many other ways to implment saving. but id say this is a good way for a beginner to keep it simple and learn from it.

and as codeSmile said, unfortunately theres no built in system for saving. you have to make one yourself or use an asset.
If you want fundamental stuff built in unreal is probably better suited. Unity is not as user friendly to new developers. But it is easier to extend and modify yourself, once you have some experience.

Additionally saving EVERYTHING is still possible and can still be done in the way i proposed. But it exponentially increases the amount of work required and the performance hit to the users pc during save and loads. even triple A games don’t save any of that stuff. dark souls doesnt even bother saving enemy locations. the level just resets on loading the game. Skyrim only saves locations of npcs within viewing distance and adjacent cells, stats and inventory objects.

Unreal has a built in system for it: https://docs.unrealengine.com/4.26/en-US/InteractiveExperiences/SaveGame/
and heres a 3rd party write up about unity: Persistent data: How to save your game states and settings
and a video guide by brackeys for unity:
https://www.youtube.com/watch?v=XOjd_qU2Ido

Please … not another “SAVE/LOAD” asset. :slight_smile:

(off-topic but can’t help it)

It currently feels like there are a lot more new assets released that try to cannibalize on existing popular (albeit not-that-hard-to-make) assets which specifically seem to aim at the beginner level and/or intend to look more valuable than they are through professional quality marketing videos/images and a high price point.

Just yesterday I scanned the new releases lists and found a new savegame tool, two new ingame consoles, a new mesh combiner and another ragdoll creator. Was happy to see a “boat character controller” - at least that’s unique. :stuck_out_tongue:

It’s a VERY well-understood process, no need to post in a forum.

There are millions of tutorials out there to help you get up to speed and understand the process yourself.

Load/Save steps:

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

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

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.