x


Is it possible to get all root transforms of the scene hierarchy?

Hey Unity folks! As the title implies I would like to find all root transforms in the scene hierarchy, via a C# script.

Here's the catch: There are a lot of children for each transform.

So far the methods we've found are:

  • Traverse all GameObjects and check if transform.parent == null... This is very slow since we have a lot of child transforms.
  • Keep track of which GameObjects are roots and add a root tag appropriately (then find them using GameObjects.FindObjectsWithTag).

Is there a more elegant way of finding all root transforms in the scene hierarchy?

more ▼

asked Nov 29 '11 at 01:16 PM

TheMorten gravatar image

TheMorten
128 8 8 15

I really want to know this also. Especially when the GameObjects are inactive, we can't normally find them. How can we traverse through thousands of GOs just to find several root GameObject ? I know that Resource.FindObjectsOfTypeAll should do the trick but it's terribly slow, why should I search the whole project just to find several root scene object ? Anyone have a better idea ?

Nov 06 '12 at 04:14 AM thienhaflash
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

no of course there is no way to quickly search thousands of things for a few specific things.

However its worth asking yourself how these objects get generated to begin with.

If these roots are really important to you you have a few choices.

  1. Search during load and store the result (lets be clear here, once you find these objects or any really you store them in a variable so you dont have to run another search again, that is irregardless of the way you search)

  2. Store them during generation, it seems to me that when a parent is created you should simply shuttle it off to an array of transforms for later access. Is there a reason this isn't viable?

keep in mind even if the parents are changing its probably less costly to mess with a small array that search a large scene.

  1. Maybe you should explain your issue in more detail and we can help you come up with an alternative way of doing something. You said each parent has lots of children, maybe thats not a good approach. It does for example result in tons of things waking up all at once on a movement. If you explained the larger context it might be that you dont in fact need what you think you need.
more ▼

answered Nov 06 '12 at 04:34 AM

sparkzbarca gravatar image

sparkzbarca
3.2k 2 5 18

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x205
x147
x26

asked: Nov 29 '11 at 01:16 PM

Seen: 745 times

Last Updated: Nov 06 '12 at 04:34 AM