- Home /
How to get first depth child components of a parent object?
Hi to all.
If i have an gameobject that has more than one child gameobjects and each of it's child also have their child.Is it possible or is any way to get transform component of all first level child ? like below diagram where each of child repeat it's parent structure and i want access transform of child 1 until child n:
gameobject
child 1
child 2
.
.
child n
Note the bit of source at the top of the file at this link:
http://docs.unity3d.com/Documentation/ScriptReference/Transform.html
Thanks for reply
I already saw that link,but my question is how to get transform of child 1,child 2 ,..and child n ,and not transform of their child or all child transform of current gameobject(since child 1 ,..,child n themselves have child objects).
Answer by s_guy · Jul 18, 2013 at 12:19 AM
A brute force way would be to iterate all of a transform's children. For each child, check to see if the the child's transform.parent is the same as the transform you started with. If it is, then it's a first level child.
You could set up a loop or recursive check for each child until the above match is made and count each recurrence to see how far you had to go.
Yes i had already thought of that,but i.e. in a complex model with very much child objects(In my current case it really is complex object,a player model with many child)whether it does not affect performance?
I figure out my problem with a different solution.indeed i'm working on an inventory system like that in "risen" game,and this question was about equiping and unequiping an item to a player body part.
But since i think there isn't no better answer for my primary question,so i mark this answer as correct,because it works.
Answer by sathya_m · Oct 09, 2013 at 09:02 AM
in C#
If you are looking for only first depth of Gameobject childs
foreach(Transform trans in object.transform)
{
Debug.Log("child"+ trans.name);
}
//////////////////////////////////////
if you want to read child form all depth of hierarchy
Transform[] transforms = Object.GetComponentsInChildren();
foreach(Transform trans in transforms)
{
Debug.Log("child"+ trans.name);
}
Thanks, I was wondering whether I was getting deeper children by mistake with the 1st formula but actually it was not the case. I guess object.GetComponentsInChildren also returns the component T of object, so you would also get the transform of the said object with the second formula.
Why does foreach(Transform trans in gameObject.transform) {}
result in getting the first-depth?
Answer by vexe · May 05, 2015 at 05:18 AM
In case someone wants to get depth values in a gameObject hierarchy:
void LogDepth(Transform root, int depth)
{
var children = root.GetComponentsInChildren<Transform>();
foreach(var c in children)
{
if (c.parent == root)
{
print(c + ": " + (depth + 1));
LogDepth(c, depth + 1);
}
}
}
eg: consider the following hierarchy:
root
-- A
---- B
------ C1
------ C2
root is at depth 0, A is at 1, B is at 2 while C1 and C2 at depth 3
Answer by Kerihobo · Jan 24, 2017 at 09:25 AM
Sorry, I know this is old, but can you simply say
foreach (Transform t in someTransform) {
if (t.parent == someTransform) {
Debug.Log(t);
}
}
Answer by V-Jankaitis · Oct 11, 2017 at 01:55 PM
public static Transform[] GetTopLevelChildren(Transform Parent)
{
Transform[] Children = new Transform[Parent.childCount];
for (int ID = 0; ID < Parent.childCount; ID++)
{
Children[ID] = Parent.GetChild(ID);
}
return Children;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613110327im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
NULL REFERENCE EXCEPTION,NULL REFERENCE EXCEPTION while accessing child objects unity2D 0 Answers
Counter for sprite under script from GameObject prefab 0 Answers
GetComponentInChildren, is there an Alternative way? 1 Answer
How do I access Mesh/ Meshfilter in Editor code? 3 Answers
What's the most efficient way to change script from child object 2 Answers