Hello All,
In lot of engine there is always a root in the game hierarchy. This can be explicit or implicit.
Is it the same in Unity3D?
eg: is there a gameobject that is the parent of any gameobject in the hierarchy?
If yes, what is its name and can it be accessed from script?
Nop. GO’s at the top of the GO hierarchy will have transform.parent set to null. If you really need a global parent, you could just do that yourself by moving all your GOs into one top-level GO.
Unity doens’t have a root gameobject. This has always been a bit hard for me to get used to as well actually. I’m used to be able to iterate over all the top level gameobjects in a scene.
Instead, what you can do is FindObjectsOfType(),
and use it to find gameobjects (or transform components, basically boils down to the same thing in the end)
I actually think it makes good sense for unity to add the concept of a scene, as right now there is no rigid line between “objects that are assets that live in my assetsfolder”, and “objects that live in my scene”, or even “objects that unity uses internally”.
Bye, Lucas
Agreed. Also, the manually parenting thing will cause some really funky issues when you have two rigidbodies needing to have the same parent.
is there any way to make a object not destroy on load? It always says it is only available on root gameObjects.
That’s actually a completely unrelated question and you should have created your own thread instead of necroing a 14 year old one. I’m pretty sure what you are refering to is that one line in the documentation of DontDestroyOnLoad, in which case it simply means you cant “DontDestroyOnLoad” a child of some parent. You must call it on the most-parent node of that hierarchy, or in other words it can only be used on top-level gameobjects in the scene view. The term “root” is used differently there than it is in this thread. This thread is about whether or not there is one singular root gameobject under which everything else is parented. The documentation in question says “works on root gameobjecs”. This simply means it doesnt work on anything that has a parent. If you want to DontDestroyOnLoad a child, you could first unparent it and then call DontDestroyOnLoad on it, since it would then itself be a root object.
2 Likes