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

SceneManager.LoadScene and FindGameObjectWithTag

Discussion in 'Scripting' started by James0oO, Feb 25, 2016.

  1. James0oO

    James0oO

    Joined:
    Dec 8, 2014
    Posts:
    10
    Hello,

    I have an issue with loading/streaming game content into current play.

    Basically if I have...

    Code (CSharp):
    1. SceneManager.LoadScene ("NewScene", LoadSceneMode.Additive);
    2. GameObject objectINeed = GameObject.FindGameObjectWithTag ("SomeTag");
    where the object with the tag "SomeTag" is within NewScene, the Find is returning nothing as though it isnt ready yet. If I put the Find out in a Coroutine with a 0.1 second delay it works fine. Is this to be expected? It just feels a little messy.
     
    Last edited: Feb 25, 2016
  2. James0oO

    James0oO

    Joined:
    Dec 8, 2014
    Posts:
    10
    Im having lots of problems with this. How can you tell when a scene is fully loaded?

    If I have a "loading" scene in place and then...

    Code (CSharp):
    1. SceneManager.LoadScene ("Main", LoadSceneMode.Additive);
    2. SceneManager.SetActiveScene (SceneManager.GetSceneByName ("Main"));
    3.  
    The SetActive does not work because the scene is not loaded yet.
    As mentioned before I can put a delayed coroutine and it works, but GameObjects that are in the "Main" scene that need to instantiate things start instantiating in the Loading screen.
     
  3. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
  4. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    You could use OnLevelWasLoaded()

    Code (CSharp):
    1. void OnLevelWasLoaded(int level){
    2. GameObject objectINeed = GameObject.FindGameObjectWithTag ("SomeTag");
    3. }
     
  5. James0oO

    James0oO

    Joined:
    Dec 8, 2014
    Posts:
    10
    Thanks for the help, I appreciate it.