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

Jump back and forth through scene and keeping the scene state

Discussion in 'Scripting' started by tbui1_unity, Jul 25, 2022.

  1. tbui1_unity

    tbui1_unity

    Joined:
    Aug 12, 2021
    Posts:
    5
    I have a project that contains different minigames, there should be the main game scene where players can go to different nodes to play minigames just like in Cuphead. There are two games of inspection trucks and drive the truck I make them in 2 different scenes. My plan is players could go inspect their truck before they can drive then the flow should go to the inspection scene and then back to the main game scene the problem is when I back to the main game scene by regular LoadScene function, it does not remain the current state where having option to inspect or drive but it just starts from the beginning. I just wanna ask how you guys control scene change when you guys want to jump back and forth through the scenes without resetting everything.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
    This sounds like a simple case of data persistence. You just need to record the necessary data from one scene as you go to the next, then restore the state using said data when you return to the scene in question.

    Naturally there's any number of ways to do this, such as throwing the information into a static class or a scriptable object. Or it could be written to disk (ergo, a save file); in fact probably a combination of both the former and latter, as they no doubt go hand in hand.

    In some cases the original scene may never need to be unloaded, too. If this truck inspection is, say, just UI, you can additive load and unload it (or just enable and disable the canvas) on top of the driving scene. I'm sure a lot of games that have this kind of back and forth just never unload the primary gameplay scene, and instead 'pause' it while something else happens on top.
     
    tbui1_unity likes this.
  3. tbui1_unity

    tbui1_unity

    Joined:
    Aug 12, 2021
    Posts:
    5
    Thank you for replying, I have tried to make additive solution but I get into trouble when the scene is not completely over the primary scene and they overlap. I keep finding how to hide a scene when I do not use it ( as you said pause it but I want it won't show too). However, my main scene always starts with a short intro scene, I wonder can data persistence prevents the intro scene appear when I go back to the main scene.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
    I imagine a lot of games use a camera stacking approach (not sure if that's the correct terminology), where they just render one scene on top of the other, rather than physical position them on atop the other. Well outside my knowledge, mind you.

    I mean... yeah it can. This is one of those 'anything is possible' scenarios, you just need to work at it to make it happen in the context of your project.

    I'll note, a lot of what you're after is more or less synonymous with save/load systems. In a lot of cases the information you care about persisting between scenes is also information you want to persist between play sessions. So perhaps tackling a save system will knock out two birds with one stone, as they say.
     
    tbui1_unity likes this.