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. Dismiss Notice

Load required scenes before Awake/Start of objects when entering play mode from level scene

Discussion in 'Scripting' started by Sangemdoko, Jul 27, 2021.

  1. Sangemdoko

    Sangemdoko

    Joined:
    Dec 15, 2013
    Posts:
    218
    Hi

    I'm working on a project with multiple scenes. One scene has some managers which are required by some objects in all levels.
    Starting from a start scene allows me to setup everything and load the required scenes in order.

    For play testing purposes though I want to be able to start play mode from any level scene. So I've added a prefab in all level scenes that loads the required scenes. I made sure that the script is executed before everything using execution order.

    The issue is that the Awake/Start functions of all the components in the Level scene are being called before the Required scenes have finished loading completely.

    My solution for now is putting the entire Level inside a parent game object and deactivate it before loading the scene asynchrounously and activating it once it is done.

    That works perfectly fine but I'm wondering if someone has a better solution?

    I know that having a flatter hiearchy is better. Is it worth getting all root gameobjects and deactivate those instead of having a single master parent? Or maybe I should remove the master parent once the required scenes are loaded?
     
  2. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,115
    You can set the startup scene via code in the editor. Works even if you hit play from any of your scenes: https://docs.unity3d.com/ScriptReference/SceneManagement.EditorSceneManager-playModeStartScene.html

    Here is what I do to have fast scene testing:
    1) Set playModeStartScene to the "main" scene which contains the bootstrap code etc.
    2) Have some editor code which saves (in editor prefs) what the opened scene was when play was hit (or you do this manually with a button or a menu entry).
    3) Have some code in the main scene which detects if there was another scene loaded (checks the editor pref from step 2). If yes then clear the editor pref and automatically load that scene after the main scene finished initializing. I #ifdef that code so it's only active in the editor.
    4) Unset/Restore playModeStartScene

    That way you will have the comfort of testing without having to manually change your scene to "main" AND you are actually going through the normal loading and setup code (testing the real thing). Caveat: you are loading the whole game, so depending on your setup it might take quite a lot longer than loading just your level.
     
    Joe-Censored likes this.
  3. Sangemdoko

    Sangemdoko

    Joined:
    Dec 15, 2013
    Posts:
    218
    Thank you! I didn't know PlayModeStartScene was a thing, I think that might be what I want.

    For debugging I also have a transform that I use to spawn the character in a specific position. I'm sure I can modify the code a bit to make it work using the editor prefs you mentioned. To know whether I should spawn in the original spawn point or a debug one.
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,357
  5. FOXAcemond

    FOXAcemond

    Joined:
    Jan 23, 2015
    Posts:
    99
    I'm curious, how do you do that?
     
  6. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,115
    The EditorSceneManager has a lot of events which you can use to store the last opened scene. In the past I stored it in EditorPrefs and then used it at runtime. Nowadays I just use a Button to set the info explicitly.
     
    Joe-Censored likes this.