Search Unity

Scene Saving and Loading using Player Prefs

Discussion in 'Scripting' started by jevanswow, May 27, 2015.

  1. jevanswow

    jevanswow

    Joined:
    Apr 17, 2015
    Posts:
    33
    I want to thank everyone for all their help and I seriously hope this is the last question I would have as far as player prefs and again I appreciate everyone help. While the documentation on the Unity website is helpful, it only briefly discusses each specific topic so it seems like to get information on player prefs I might have to search more then one topic.

    Anyway with help from a community member I was able to put in the transform position on player prefs with saving and loading and I wish I would remember to ask this question but I forgot. As far as saving and loading scenes goes how would I be able to code the current scene on saving and then be able to load that scene. So lets say I want to save the game in my starter town, travel to a another town lets say it takes 5 minutes to get to that town and then if I realize I want to get something in that starter town instead of walking back just go ahead and load.

    I'm sure you guys will give me the code but if anyone can give me the code to detect the current scene that will be helpful as well which was I was mainly looking for but the closest thing I found was Application.loadedLevelName.

    Here's the saving code

    if confirm == true)

    {
    GUILayout.Label (gamesaved [0]);

    if (GUILayout.Button (acknowledged [0]))

    {
    confirm = false;

    PlayerPrefs.SetString ("Class", playerstats.Class);
    PlayerPrefs.SetString ("Rank", playerstats.Rank);

    PlayerPrefs.SetFloat("PlayerX", transform.position.x);
    PlayerPrefs.SetFloat("PlayerY", transform.position.y);
    PlayerPrefs.SetFloat("PlayerZ", transform.position.z);

    }
    }

    And the loading code

    GUILayout.Label (loadquestions [0]);

    if (GUILayout.Button (choices [0])) {
    DisplayDialogLoad = false;
    playerstats.Class = PlayerPrefs.GetString ("Class");
    playerstats.Rank = PlayerPrefs.GetString ("Rank");
    transform.position = new Vector3(PlayerPrefs.GetFloat("PlayerX"), PlayerPrefs.GetFloat("PlayerY"), PlayerPrefs.GetFloat("PlayerZ"));
    }

    As always I appreciate the help.
     
  2. jevanswow

    jevanswow

    Joined:
    Apr 17, 2015
    Posts:
    33
    On second thought I might just code the save where it saves everytime the player enters a new scene/level. For example When the player goes from the starter town into the warrior class hall it'll save, when the player goes from the warrior class hall to the starter town it'll save. Please don't think I'm just taking the easy way out because I'm not I'm just trying to figure out the best way to do this.

    Anyone with experience with releasing games to the public preferrably with more than 3 levels (I think the technical term is level but I just call it scenes) if they can let me know which way they do it that would be perfect.

    Thanks