Search Unity

Saving player location in only main scene.

Discussion in 'Scripting' started by cruising, Nov 7, 2018.

  1. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    Hello!

    I have a main space scene that contains plenty of star systems where the player can go to.
    Is there any script tutorial or asset that i could use to save the players location in only the main scene when entering a star system (other scene) and load it back when exit the system to the main scene?

    Each star system in the main scene has a bigger sphere collider that tells you on the screen what system you are at. Maybe that collider also could trigger a saving function so when you exit the star system scene you spawn somewhere in that collider?

    Thank you.
     
  2. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    450
    Step 1: Figure out if you are in the correct scene using SceneManager.GetActiveScene(), you could e.g. use the build index attribute.
    Step 2: Use - as a simple start - PlayerPrefs to save the attributes, either as a complete string or as several keys.
    Step3: When a scene is loaded (SceneManager.sceneLoaded) check again if it is your Main-Scene and load the previously saved PlayerPrefs.
     
  3. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    725
    When you deal with reading and writing data I'll add that if you plan on having it compatible with Android don't use StreamingAssets path instead of PersistentData path.

    If you want it to work with WebGL use IEnumerators for saving and loading files or the browser will lock up.

    I think what you're looking for is creating your own scene manager that reads and writes data from a main database that contains saved position and rotation? That's what I do at least...I have a main game database that saves player position and rotation any time they leave a scene. You need like a method that is like Scene_Goto(string sceneName, then an optional string gameObjectToSpawnAt, optional string cameraToStartAt).

    This can be one of the most time consuming parts of development, transitions between scenes and writing and reading of data.