- Home /
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 :P
Thanks
Klarax
You might want to look up what serialization actually is, because (like most things) if something is given a same, it actually does what the name implies.
Also note that Unity doesn't have a "save" functionality built in.
Answer by Superrodan · Oct 03, 2014 at 10:54 AM
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: http://docs.unity3d.com/ScriptReference/PlayerPrefs.html
I'd like to point out that I'm a Unity beginner so if someone who knows better comes along I'd be happy to be corrected.
PlayerPrefs
(as the name suggests) should be used to save preferences. This is especially the case on windows, where it saves to the windows registry (basically a giant mess of a database for preferences). This also makes it a lot more difficult to debug.
This question has some better suggestions.
Your answer
Follow this Question
Related Questions
Newbie questions! (Variables and Saving) 2 Answers
Serializing a graph 2 Answers
Serializer Script 3 Answers
how to properly save an int value ? 2 Answers
I am getting a serialization exception error when trying to save and load in Unity? 2 Answers