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!

The answers about this are quite thorough, even if mine isn’t.

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.


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: Unity - Scripting API: PlayerPrefs

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. :confused: