Search Unity

Save coords when exit main scene and load when back

Discussion in 'Scripting' started by cruising, Apr 19, 2020.

  1. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    Hello!

    I have tried looking for some tutorials how to save coordinates when exit main scene and load the position when you load the main scene again.

    I have a deep space scene with several star systems that you can enter and exit, but need some help to make the player spawn back at the same star system when leaving the actual star system scene and out to deep space again.

    Maybe it can be combine with my position logger script?

    Code (CSharp):
    1. public class PositionLogger : MonoBehaviour
    2. {
    3.     public GameObject Player;
    4.     public Text PositionX;
    5.     public Text PositionY;
    6.     public Text PositionZ;
    7.  
    8.     void Start ()
    9.     {
    10.  
    11.     }
    12.  
    13.     void Update ()
    14.     {
    15.  
    16.         LogMyPosition ();
    17.     }
    18.     public void LogMyPosition()
    19.     {
    20.         PositionX.text = " X: " + Player.transform.position.x;
    21.         PositionY.text = " Y: " + Player.transform.position.y;
    22.         PositionZ.text = " Z: " + Player.transform.position.z;
    23.     }
    24. }
    I also have been trying with this coding, but it does not work as i want it to do, it seams to save coord in any scene and make you spawn in main scene at the same position you were at in the star system scene.

    Code (CSharp):
    1. public class SavePosition : MonoBehaviour
    2. {
    3.     public GameObject PlayerShip;
    4.     public string sceneName;
    5.  
    6.  
    7.     void Start()
    8.     {
    9.         Scene currentScene = SceneManager.GetActiveScene();
    10.         sceneName = currentScene.name;
    11.  
    12.         if (sceneName == "AdminScene")
    13.         {
    14.             Vector3 savedPosition = new Vector3(PlayerPrefs.GetFloat("playerX"), PlayerPrefs.GetFloat("playerY"), PlayerPrefs.GetFloat("playerZ"));
    15.             PlayerShip.transform.position = savedPosition;
    16.         }
    17.     }
    18.  
    19.     void OnLevelWasLoaded(int level)
    20.     {
    21.         Scene currentScene = SceneManager.GetActiveScene();
    22.         sceneName = currentScene.name;
    23.  
    24.         if (sceneName == "AdminScene")
    25.         {
    26.             PlayerPrefs.SetFloat("playerX", PlayerShip.transform.position.x);
    27.             PlayerPrefs.SetFloat("playerY", PlayerShip.transform.position.x);
    28.             PlayerPrefs.SetFloat("playerZ", PlayerShip.transform.position.x);
    29.         }
    30.     }
    31. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Is this a question about loading / saving game data? There's a lot of tutorials on that...

    Is it a question of persisting data between scenes? Generally you either make a GameManager that lives for the necessary duration of time, or else save things to static variables and reconstruct them when you arrive at the next scene.
     
  3. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    All i want to do is to save the players last position in the main scene, and load that position next time the player enter the main scene again.
     
    Last edited: Apr 20, 2020
  4. Gnatty

    Gnatty

    Joined:
    May 17, 2012
    Posts:
    77
  5. Leonetienne500

    Leonetienne500

    Joined:
    Dec 5, 2016
    Posts:
    130
    No, he does not. He wants so save it for when the scene loads again.

    Save it in the player prefs. Actually, don't do this for actual deployment because it can be "hacked" easily. But for testing it will work just fine. Quick and dirty.

    Code (CSharp):
    1. PlayerPrefs.SetFloat("MyXPos", 420);
    2.  
    3. if (PlayerPrefs.HasKey("MyXPos") {
    4. float loadedXPos = PlayerPrefs.GetFloat("MyXPos"); }
    5.  
    Load it in, obviously, Awake() and save it JUST before the next scene loads.
    You are most likely calling the SceneManager.LoadScene yourself anyways, so just call the save method just before.

    For actual deployment use object serialization.

    Mind though: Playerprefs are global. Maybe a better key name would be something like "MapGarden/playerX"

    Also mind: Awake and Start get only called ONCE per object load. So, if an object is tagget for "DontDestroyOnLoad", its delegates Awake() and Start() will NOT be called again
     
    Last edited: Apr 20, 2020