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

Problem with loading scene asynchronously and creating objects on Awake

Discussion in 'Scripting' started by CRiess, Jul 21, 2017.

  1. CRiess

    CRiess

    Joined:
    Jul 11, 2017
    Posts:
    16
    I created a system with a scene loader, which loads scenes asynchronously, then unloads the previous scene. The scene loader "lives" in its own scene "SceneLoader", but gets marked as "DontDestroyOnLoad", so it is removed from the actual scene hierarchy. So when I start the game with the scene loader scene as a start scene, in the hierarchy I have the "SceneLoader", which has no objects, and the "DontDestroyOnLoad" section, which houses the GameObjects of my scene loader.
    In the scenes I am loading, there are several scripts, that create GameObjects in the Awake function. When this happens, they do not get placed into the newly loaded scene, but into my "SceneLoader" scene. It seems like Awake is called before the new scene is completely initialized. I am currently using a workaround and move the misplaced objects back to the scene where they belong, but this seems like a hack and not intended behaviour.
    I am using Unity 5.4. Is this a known bug in this version or is this intended behaviour? Am I doing something wrong?
     
  2. goonter

    goonter

    Joined:
    Aug 31, 2015
    Posts:
    89
  3. CRiess

    CRiess

    Joined:
    Jul 11, 2017
    Posts:
    16
    Ok, so if I understand that correctly, there is no way, to switch the active scene *before* Awake() is called on the objects of the new scene, since it is technically not loaded and can not be set to active before? So I can only help myself with the workaround of moving the objects, and then set the scene to active after that, correct?