Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

switching scenes without loading them again

Discussion in 'Scripting' started by lakiid373, Jul 9, 2019.

  1. lakiid373

    lakiid373

    Joined:
    Jun 11, 2019
    Posts:
    5
    So i have a big problem,i don t even know if its possible to solve.I have 2 scenes 1 scene is the main menu 2 is the game, the 2 scene is a lvl generator it has nothing and when it starts it generates random levels.Now lets say that the player enters the game, he presses play and he gets to scene 2 a random lvl is generated for him,he plays and when he dies he can return to main menu, he presses return and he returns he changes some skins etc.AND now the problem when he returns the second scene will be loaded again and another lvl will be generated(and i want that when he returns, he returns to the lvl he left) i think that if i load both scenes in the background form start (menu) then when he will switch between scenes, he will switch between the same scenes with the same generation cuz the generator works only when the scene is loaded.
    Thx in advance.
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Don't bother with that. Better reload your generator scene for clearance.
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I'd load scenes additively instead of completely switching scenes. When you are in the main menu scene just have all the GameObjects of the level generator scene disabled and the main menu scene set active. When playing the game, you have the GameObjects of the main menu scene disabled and the level generator scene set active. I'd also change it so a level isn't generated automatically on scene load, but instead by a script calling for a level to be generated.

    Note that doing the above will likely increase your load times on game start, so you might also want to create a splash screen or loading scene that you play while loading the 2 other scenes.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    @Joe-Censored had a similar idea to what I was thinking. The other option is if you have the data related to the generated scene, just keep that and use it to regenerate the level scene instead of creating a new one. You could add some logic in there to handle when you need a new one vs when to reuse.
     
    Joe-Censored likes this.
  5. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Also worth noting when doing additive loading that you may want to use a "root" object for each scene (which all other objects are under) so that the entire thing can be disabled and enabled as needed when switching around without having to iterate over a list of GameObjects.

    Eventually, after you've gotten a fair grip on how scenes work conceptually, you may want to just move to a "one scene game" approach and load and unload everything dynamically without using scenes at all, and instead just prefabs based on game states. Scenes and prefabs don't really differ that much in implementation, and with object pooling you may find that very little of the actual "scene" is scene-specific, and could potentially be re-used in various places. Once you've separated out all of that for efficiency, the rest is usually pretty easy to move into a prefab rather than having a scene dedicated to it.

    The only big issue I've encountered is with baked lighting data, and that's probably going to be just as annoying to manage using additive scenes IMHO.
     
    Joe-Censored likes this.