getting scene top level objects

Hi again!
How do I get all the top objects of the scene?

FindObjectsOfType(x) is no good because it returns the children as well.
can’t use .Find(“some name”) because I want to be generic and find just any game object at hierarchy top level.

is there any function to accomplish this?

I got around it like this, but this is not optimal.:?

var list = FindObjectsOfType(GameObject);
for (var obj : GameObject in list) {
        if(!obj.transform.parent){
// at top level
        	ProcessObject();
}
}

Why?

well, unnecessary overhead.
first of all unity is making a list of all abjects which is a waste of processing power and then I have to run another loop to find out which are the ones at the top level to then go in and process the objects in the order I need.

it would be a lot nicer if I could do something like this.

for (var obj : GameObject in scene)
      ProcessObject();

easier to read, no list creation overhead, no if statement and an easier way to get to what I’m looking for.

the project is already organized as a scene, why not give access to it’s contents?

1 Like

Just put all the transforms you wnat listed under one parent, and you can loop through that parent’s children.

(Surely you don’t want EVERY root transform - what about the objects holding your GUI, etc, etc?)

thanks StarMarta, that’s what I did with GameObject.Find(“rootObject”)
I think that’s the best way to do it, I can live with that :smile: