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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Additive Scene Load - Objects get instantiated in "old" Scene

Discussion in 'Scripting' started by mrCharli3, Apr 23, 2018.

  1. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    956
    When moving from one scene to another, I fade to black, do the scene unload/load, fade in.

    This works nicely, however if I have a Script in the "new" Scene that is being loaded that tries to Instantiate someting in Start() or Awake(), that thing gets instantiated in the "Old" scene, i.e the scene I'm currently unloading. I assume because this scene is technically still set as "active". The "new" Scene won't be set to active until it is fully loaded. Is there any way I can get around this? So that the Objects get Instantiated in the correct Scene?

    Code (CSharp):
    1.  
    2.  
    3. yield return StartCoroutine(FadeOutScene());
    4.  
    5. private IEnumerator UnloadAsyncScene(string scene)
    6.     {
    7.         AsyncOperation asyncUnload = SceneManager.UnloadSceneAsync(scene);
    8.  
    9.         Debug.Log("Unloading Scene: " + scene + "...");
    10.  
    11.         while (!asyncUnload.isDone)
    12.         {
    13.             yield return null;
    14.         }
    15.  
    16.         Debug.Log("Finished unloading Scene: " + scene);
    17.     }
    18.  
    19. private IEnumerator LoadAsyncSceneAdditive(string scene)
    20.     {
    21.         AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(scene, LoadSceneMode.Additive);
    22.  
    23.         Debug.Log("Loading Scene: " + scene + "...");
    24.  
    25.         while (!asyncLoad.isDone)
    26.         {
    27.             yield return null;
    28.         }
    29.      
    30.         Debug.Log("Finished loading Scene: " + scene);
    31.  
    32.         SetActiveScene(scene);
    33.     }
    34.  
    35. StartCoroutine(FadeInScene());
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,186
    Unity doesn't provide any overload for Instantiate that specifies the scene to put the object in. So you have three alternatives:

    1: Set the new scene as active before you instantiate. This has major downsides (like changing the light settings and such)
    2: Move the objects to the new scene after instantiating them by using SceneManager.MoveGameObjectToScene
    3: Instantiate the objects as children on an object in the new scene by passing the parent object to Instantiate.
     
    mrCharli3 and Nigey like this.
  3. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    956
    Amazing, option 3 sounds like the way to go!