Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Saving and Loading Player Position/Rotation etc

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

  1. jevanswow

    jevanswow

    Joined:
    Apr 17, 2015
    Posts:
    33
    Hi all

    I am working on a script that will save and load data and I didn't have any problems with saving/loading simple variables/values like a player rank using player prefs but unfortunately while if I remember right I should look up Player Prefs X for it unfortunately that's all I know.

    I hate to just ask for code without showing effort on this but unfortunately after searching yahoo and youtube I wasn't able to find anything that I need and therefore if anyone would be kind enough to give me the code for saving a player position and then loading I would certainly appreciate it a million times. :)

    Thanks so much.
     
  2. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    An easy way of doing this would be to define each values of the position and rotation (x, y, z) to a PlayerPrefs variable.

    Code (csharp):
    1. //Saving
    2. PlayerPrefs.SetFloat("PlayerX", player.transform.position.x);
    3. PlayerPrefs.SetFloat("PlayerY", player.transform.position.y);
    4. PlayerPrefs.SetFloat("PlayerZ", player.transform.position.z);
    5.  
    6. //Loading
    7. transform.position = new Vector3(PlayerPrefs.GetFloat("PlayerX"), PlayerPrefs.GetFloat("PlayerY"), PlayerPrefs.GetFloat("PlayerZ"));
    8.  
    And Doing the same for the rotation values...

    If you want to avoid the PlayerPrefs, look here:
    http://unity3d.com/learn/tutorials/...ining-archive/persistence-data-saving-loading

    That Unity learn lesson is great. But depending on what you want to do, simply using PlayerPrefs is less of a hassle.
     
    Last edited: May 26, 2015
  3. jevanswow

    jevanswow

    Joined:
    Apr 17, 2015
    Posts:
    33
    Thank you for helping out with that. :)
     
  4. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
  5. jevanswow

    jevanswow

    Joined:
    Apr 17, 2015
    Posts:
    33
    I'm not sure what's going on and I certainly appreciate the help but when I use the player.transform.position.x it says that player doesn't exist in the current context. I was able to just use transform.position.x for saving and that fixed it but I'm still having a problem with the loading part. What I'm trying to do with the save and load will be once the player clicks the ok button on saving/loading it'll do some stuff. Anyway here's the code

    Saving:

    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);
    }
    }

    Loading:

    if (GUILayout.Button (choices [0])) {
    GameObject go = GameObject.FindGameObjectWithTag ("Player");
    DisplayDialogLoad = false;
    playerstats.Class = PlayerPrefs.GetString ("Class");
    playerstats.Rank = PlayerPrefs.GetString ("Rank");
    transform.position.x = PlayerPrefs.GetFloat("PlayerX");
    transform.position.y = PlayerPrefs.GetFloat("PlayerY");
    transform.position.z = PlayerPrefs.GetFloat("PlayerZ");
    }

    The error messages I get is with the transform.position on the loading where it says cannot modify the return value of UnityEngine.Transform.position because it is not a variable so my guess is I need a Vector3 or a game object in there but also it would just be a guess.

    Also as I stated in another post while I am learning a lot and certainly more than I did before (this is my first game project) I'll admit that I'm totally clueless on some of this stuff and the problem I'm running into is I mainly don't know which resources to use. I have spent over $50 on Unity Books that shows examples but between those and the resources I do find I already know about, I'm having a hard time finding resources on while I'm not sure if player prefs is intermediate but between that and a few other stuff I can't find resources on so if anyone knows where to start I would certainly appreciate it.
    Thanks
     
  6. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    @jevanswow,

    Pasting Code
    [CODE ]When pasting code on the forum, make sure you to put the script in between these tags, without the spaces.[ /CODE]

    player
    It says player doesn't exist because it does not exist. In my snippet, player is thought to be a reference to the player.
    If you put the saving/loading script on your player, you are right to target the player with transform.position only.

    Oups, I made a mistake with the previous code.
    Code (csharp):
    1. transform.position = new Vector3(PlayerPrefs.GetFloat("PlayerX"), PlayerPrefs.GetFloat("PlayerY"), PlayerPrefs.GetFloat("PlayerZ"));
    2.  

    Books/Learning
    Book are ok. But remember that any software evolves quickly.
    The thing is, by the time a good book for Unity5 will be release, Unity 5.1 will be release and will have new features, etc. Anyway if you have a pre-Unity5 book, I would be carefull.

    The best resources to learn, are essentially here in the Unity Learning section.
    http://unity3d.com/learn/tutorials/modules/beginner/scripting

    Also, there are a bunch of great tutorials series on Youtube that are worth a watch, after you are done with the basic tutorials here.
     
  7. jevanswow

    jevanswow

    Joined:
    Apr 17, 2015
    Posts:
    33
    Woohoo works like a charm. Thank you so much :)
     
  8. Creer_LLC

    Creer_LLC

    Joined:
    Feb 28, 2017
    Posts:
    15
    I have a similar issue, i have 20 child transforms with a method which after a drag and drop collects the transform.name of each and send as a string to the webserver but i want to be able to save the last arrangement of the child transforms in playerprefs so anytime the player logs in, at start(), the last played game arrangement would be the start arrangement.

    Code (csharp):
    1.  
    2. public Transform[] position;
    3. string temp;
    4.  
    5.     void Submit ()
    6.     {
    7.         temp = "";
    8.         position = new Transform[transform.childCount];
    9.         int i = 0;
    10.         foreach (Transform t in transform)
    11.         {
    12.             position[i++] = t;
    13.             //result.Add(t.name);
    14.            temp += t.name;
    15.            
    16.         }
    17.     }
    18.  
     
unityunity