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

Need Help With Going from One Scene to Another and Then Back to Previous Scene

Discussion in '2D' started by Sebjschindler, Apr 3, 2017.

  1. Sebjschindler

    Sebjschindler

    Joined:
    Jan 5, 2016
    Posts:
    2
    Hello everyone,

    I am new to Unity and I am making a game that has 2 segments: An overworld segment and a side-scrolling segment. In the overworld segment you perform actions that will trigger the side scrolling segments. I know how to transition from the overworld segment scene to the side scroll segment scene, but I do not know how I would be able to revert back to the previous scene without starting from the very beginning of the level and retain any actions I did. For example, if in the overworld I need to hack into a terminal, I would go up to it, press the corresponding button(E), and then it loads the hacking segment. Once the hacking segment is done I would like to revert back to the previous scene at the terminal, not the beginning of the level with the terminal hacked. I do not know if my wording makes 100% sense to anyone, but I really need to know. Please ask any questions you would like.
     
  2. LiberLogic969

    LiberLogic969

    Joined:
    Jun 29, 2014
    Posts:
    138
    A simple solution is to create a singleton script that holds all of your persistent data for your overworld scene and then pass it to the DontDestroyOnLoad method. This will keep that GameObject around when scenes transition.

    You could set it up so before you transition to the sidescrolling scene you take a "snapshot" of the overworlds data and store it in that script.

    When you transition back to the overworld use that data to set everything back to the way it was and place the player to his/her/it's last position.

    You could also look into serializing the state of your overworld with JSONUtility or the binaryformater.

    https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
    https://docs.unity3d.com/Manual/JSONSerialization.html
    https://unity3d.com/learn/tutorials/topics/scripting/persistence-saving-and-loading-data
     
    Last edited: Apr 4, 2017