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

Bug: GetRootGameObjects() is not working in Awake

Discussion in 'Editor & General Support' started by ArtyBrest, Jan 13, 2016.

  1. ArtyBrest

    ArtyBrest

    Joined:
    Feb 20, 2014
    Posts:
    10
    Hi.
    I found that GetRootGameObjects is not working when called from Awake. Unity error: "ArgumentException: The scene is not loaded."
    I think it's a bug. Because actually root game objects are exist in scene at this moment and I can get them using nontrivial way.
    Could someone pass this to dev team?
    Thanks.

    Unity v5.3.1p2
     
  2. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    604
    ARRGGHHHHHHH. Pain. Such a pain for me now!!!
     
  3. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    Not really a bug. More a precaution.

    We will just double check that this is safe to call, then we can remove this one restriction
     
  4. konst3d

    konst3d

    Joined:
    Jan 30, 2016
    Posts:
    1
    The same problem but within OnLevelWasLoaded() method.
    I'm trying to find game objects of type Text to localize them on level loading complete, something like this:

    var scene = SceneManager.GetActiveScene();

    foreach (var @object in scene.GetRootGameObjects())
    {
    var components = @object.GetComponentsInChildren<Text>(true);

    foreach (var text in components)
    {
    if (IsResourceId(text.text))
    {
    text.text = GetText(text.text);
    }
    }
    }

    Any other ways to find objects of some type in OnLevelWasLoaded()?
     
  5. eventropy

    eventropy

    Joined:
    Oct 4, 2012
    Posts:
    250
  6. Deleted User

    Deleted User

    Guest

    I believe the solution is we should treat the scene which has been loaded from file but not awaked as loaded. For now, we only treated a scene as loaded after awaking.

    We're aware of this issue and planned for it, but cannot promise in which version this will be fixed.
     
  7. wihu

    wihu

    Joined:
    Nov 22, 2012
    Posts:
    25
    Hi, I also found a strange bug related to Scene.GetRootGameObjects().
    I tried to find a specific component in one of the root objects:

    Code (CSharp):
    1. Scene loadedScene = SceneManager.GetActiveScene ();
    2. foreach (var root in loadedScene.GetRootGameObjects())
    3. {
    4.     ISomeInterface s = root.GetComponent<ISomeInterface>();
    5.     s.SomeInterfaceMethod ();
    6. }
    here root.GetComponent<ISomeInterface>() returns null even though it got the correct game object.
    however, if I do it with another way:
    Code (CSharp):
    1. GameObject rootObject = GameObject.Find ("SomeRoot");
    2. ISomeInterface s = rootObject.GetComponent<ISomeInterface> ();
    3. s.SomeInterfaceMethod ();
    This works just fine in the same function scope i.e. it returns ISomeInterface component that im looking for.
    I printed both instance ids of the two game objects returned from both methods and they are exactly the same.
    I wonder if I'm missing something or its a bug in Unity?
     
  8. cxode

    cxode

    Joined:
    Jun 7, 2017
    Posts:
    268
    In 2019.2, GetRootGameObjects() from Awake() works as expected for all scenes in order until the script it was called from. All scenes after that return no game objects.

    To fix, move that scene to the end of the list.