Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Getting the root objects of a scene

Discussion in '5.3 Beta' started by TJHeuvel-net, Nov 17, 2015.

  1. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    413
    It seems we have a Scene.rootCount variable, but how can we obtain a reference to the root objects of a scene?
     
    Jodon likes this.
  2. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    413
    Scene.rootObjects might be a bit problematic, for example when you reparent them. What if the LoadSceneAdditive call returns the list of objects it has loaded?
     
  3. jpease

    jpease

    Joined:
    Apr 18, 2013
    Posts:
    11
    I second both of these requests. This is something that has always been strangely missing, and my guess is that those behind Unity's design are afraid of people misusing this particular feature in order to circumvent the way the engine is supposed to be used. But there are plenty of legitimate uses for it, the alternative being to use all sorts of ugly and/or dangerous hacks to work around the incomplete API. Scene.rootObjects would be a fine place to finally expose this functionality (and LoadSceneAdditive is one of several places where it makes sense to want that list of objects).

    As a side note, part of the reason this is such a big problem is that Unity also doesn't give us any way to check whether an object belongs to a scene or any scene. I would be fine with the addition of "bool Scene.Contains(GameObject)" or even simply "HideFlags.IsInAnyScene", as a compromise. Though Scene.rootObjects would be nicer.
     
  4. Jodon

    Jodon

    Joined:
    Sep 12, 2010
    Posts:
    417
    Noooo. They have that already (it's part of the Transform) but the issue is that there's no efficient way to get all of the objects in a Scene (which is I rely on heavily). You have to do Resources.FindObjects<Transform>() and then check each and every Transform what scene it belongs to. We seriously need a Scene.rootTransforms. Since the new LoadScene functionality returns a Scene, that solves the request of LoadLevelAdditive returning a list of objects.
     
  5. jpease

    jpease

    Joined:
    Apr 18, 2013
    Posts:
    11
    Ah, I see, they added Transform.scene. So getting all objects in a scene just went from "slow and dangerous hack" to "slow but possible and supported". Which is good enough for me, if Transform.scene works as expected. But yes, it feels very silly to be looping through all internal objects just to get at the objects in a scene. Scene.rootTransforms would be better.
     
  6. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    413
    I'm not sure Scene.rootTransforms would be the way to go, what if you change the parent of one of them? That is why i suggest returning the objects loaded in a scene.
     
  7. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,301
    If you change the parent then it's no longer in the list of root transforms. I'm not sure I see what the problem is.