Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

How to save the scene the player is in

Discussion in 'Scripting' started by Pw4, Apr 2, 2021.

  1. Pw4

    Pw4

    Joined:
    Apr 2, 2021
    Posts:
    5
    I have a very simple game, I want a simple saving mechanic that saves the game when the player makes it to a new level (scene) But I dont want it to save the position. I want it to load the scene that the player was in before they logged off.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    One really simple strategy for this is to save just the build index of the last scene the player started. This will break if you rearrange the scenes of course.
     
    Joe-Censored likes this.
  3. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    Save and load the scene string in PlayerPrefs:

    SceneManager.GetActiveScene().name


    Use a static string variable so it persists across all scenes, and just update it each time a new scene is loaded (can be in any object's Start method).
     
    Joe-Censored likes this.
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Just a note, instead of the limitation @PraetorBlue mentioned about scene indexes changing, you'll instead need to avoid ever renaming the scenes in this case. Either way will work fine though.

    A 3rd way would be to create your own identifier for each scene, save that instead of the name or index, and run it through a dictionary or switch statement to convert the identifier to the name or index of the scene to load. That way you would be free to mess around with scene indexes or scene names all you want, so long as you updated the code to reflect the changes, and not break the save/load system for existing players. But it just depends on how much code you want to write for this thing or what limitations you are fine with.
     
    seejayjames likes this.
  5. Pw4

    Pw4

    Joined:
    Apr 2, 2021
    Posts:
    5
    Would I be able to rename a new scene before I add it to my build?
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    The issue would be with renaming a scene after a player already has it as their saved last scene. In that case calling LoadScene on the now incorrect scene name would fail.
     
  7. Pw4

    Pw4

    Joined:
    Apr 2, 2021
    Posts:
    5
    ahh makes sense , thank you for taking your guys’ time out of your way to help me out
     
    Joe-Censored likes this.