- Home /
Saving Levels
Hello again, what im trying to do is create a save and load option, how would i go about doing this so that when they load it all the objects, the economy, and the time within the game is the same as it was when you saved it. Im still learning how to code so please explain it thoroughly. Thanks in advance!
Answer by Chimera3D · Jul 01, 2012 at 02:12 AM
Well saving the entire scene isn't going to happen, instead determine exactly everything that you want to save. The easiest way to go about this it to use player prefs. To write the variables to the player preferences file you would use something like this.
PlayerPrefs.SetInt("Health", playerHealth);
The Health variable is the one being saved to the file and the playerHealth variable is the one in-game. However we are setting the Health thats saved to the playerHealth. Reading these variables is just as easy.
PlayerPrefs.GetInt("Health");
Will get the health variable from the file for usage. Comparing a player pref in an "if" statement would look like this.
if(PlayerPrefs.GetInt("Health) >= 100){
//do something
}
You can't say...
if(PlayerPrefs.GetInt("Health, 100){
//do something
}
It won't work. Trust me, I had a lot of problems with this a while ago. That's just how to read and write to the files, you can also delete a player pref or delete all of them (like resetting the game). You can also have player pref strings (like the players profile name, ect.) For the rest of the documentation on player prefs: http://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.html
EDIT: I didn't realize you asked another question, sorry.
You can do that by saying:
PlayerPrefs.SetFloat("LocationX", transform.position.x);
PlayerPrefs.SetFloat("LocationY", transform.position.y);
PlayerPrefs.SetFloat("LocationZ", transform.position.z);
Then when loading:
transform.position.x = PlayerPrefs.GetFloat("LocationX");
transform.position.y = PlayerPrefs.GetFloat("LocationY");
transform.position.z = PlayerPrefs.GetFloat("LocationZ");
That's all there is to it!
EDIT (again): Saving each character is theoretically possible, but that may not be necessary. If you really need to save each character then you're going to have to make a separate set of player prefs for each character. :/
Thanks! im making a rts game so how would i save the locations of each character
You are supposed to mark an answer as correct when you get a correct answer, just so when someone else has the same question they'll know which was the correct answer.
ok its marked now how would i go about saving the locations of the characters
This is totally slow though, so you should only use it if you're not saving that much information. The values saved using PlayerPrefs
are flushed EVERY time you set something, which can be very slow if you happen to save that many things.
Answer by Berenger · Jul 01, 2012 at 12:02 AM
http://lmgtfy.com/?q=unity+save+game
The answers about this are quite thorough, even if mine isn't.
the 3 top searchs ive already looked at, they were alittle difficult to understand since im still new to this, i was hopeing someone could help me out but so that i could also understand it
You can certainly use my Unity Serializer to do that, which is the third link the search returned by @berenger's reply. Although it is never easy US makes it as straightforward as possible.
Your answer
Follow this Question
Related Questions
Newbie questions! (Variables and Saving) 2 Answers
How do you save a player's prefered resolution in Unity 2017.1.1f? 0 Answers
Saving and loading to/from Android 1 Answer
Saving and Loading 0 Answers