Saving Game Confussion

Hello,

I have been using Unity for 1 year and as of now, have yet to try and save a game.

I have a few questions, that I’m not to sure about relating to saving that I was hoping you could answer.

  1. does any class that have is serializable automatically get saved? or is this line just telling unity that it can be saved, and then has be to saved manually by another script. (hope that makes sense)

  2. do i need to get reference to each script to save it, or can I get the GameObject to save (and the script gets saved automatically because its set to serializable.

  3. does each script that is serializable need a save function to be called?

  4. how does the main save function work - i.e. does it call every script that is serializable as mentioned in point 3. or is it a much smaller function, and unity sorts out everything in the background (kinda).

Well i hope all that makes sense. Its hard to talk about things when you not sure how things work to begin with…

Any answers are welcome, since at this moment in time, my head hurts from reading :stuck_out_tongue:

Thanks

Klarax

The easiest way to save things, I’ve found, is saving to PlayerPrefs. To save an integer to PlayerPrefs, for example, you would use this line of code:

PlayerPrefs.SetInt("Player Score", 10);

This would save a value of 10 to PlayerPrefs under the name “Player Score” for you to collect later. You can save ints, floats, bools, and other things.

Additionally, by using a script found here:

http://wiki.unity3d.com/index.php/ArrayPrefs2

you can save arrays to player prefs. So if you can store variables into an array and create a load function that goes through the array and uses the variables to restore your game, that would be one way to save a game. Remembering all the things you need to save and creating a script that loads those things and restores a game can be daunting, but it’s the easiest way to do it that I’ve found.

For a bit more information about PlayerPrefs, check here: