- Home /
How can I access the children of a Transform?
I can get the parent of a Transform by using transform.parent, but how do I get the children?
I can use transform.childCount to get the number of children, but how can I access them?
Answer by duck · Jan 21, 2010 at 10:38 AM
Here is the code in both Javascript and C# for iterating through each of the immediate children of your gameObject's transform:
Javascript:
for (var child : Transform in transform) {
// do whatever you want with child transform here
}
C#:
foreach (Transform child in transform)
{
// do whatever you want with child transform object here
}
However, the above code will only give you the transform's immediate children. If your transform has a number of levels of hierarchy beneath it, you may want to access every object beneath it. In this case, you can use "GetComponentsInChildren", like this:
Javascript:
var allChildren = gameObject.GetComponentsInChildren(Transform);
for (var child : Transform in allChildren) {
// do whatever with child transform here
}
C#:
Transform[] allChildren = GetComponentsInChildren<Transform>();
foreach (Transform child in allChildren) {
// do whatever with child transform here
}
Note however, that results from GetComponentsInChildren will also include that component from the object itself. So the name is slightly misleading - it should be thought of as "Get Components from Self And Children"!
Already did that few $$anonymous$$ ago, just came back to delete this one.
@$$anonymous$$arkD:
You probably want to set child.localPosition. child.position sets a world position which gets translated into the local position behind the scene.
ps: You should also read ducks post carefully. GetComponentsInChildren will also include the Transform of the parent object as well, also keep in $$anonymous$$d that you iterate over all Transform components which are a child or a child of a child. It iterates over the whole hierarchy downwards.
Usually the first solution in ducks post is the preferred one. GetComponentsInChildren is only used in special cases.
The javascript version will issue a warning for implicit downcast. Is there an elegant way to solve this?
Answer by Mark-Davis · Aug 08, 2011 at 07:33 AM
This C# class will extend all GameObjects to allow easy walking of an entire transform tree. It's inclusive, so it hits the root first, and then all the children, as deep as they go. If this looks strange, or too good to be true, google C# extension methods.
Example usage for any GameObject in any other script.
myGameObject.VfxWalk(o => o.hideFlags = HideFlags.HideAndDontSave);
or another example with more robust syntax:
myGameObject.VfxWalk((o) => { Debug.Log(o.name); });
public static class GameObjectUtils
{
public static void VfxWalk(this GameObject o, System.Action<GameObject> f)
{
f(o);
int numChildren = o.transform.childCount;
for (int i = 0; i < numChildren; ++i)
{
o.transform.GetChild(i).gameObject.VfxWalk(f);
}
}
}
+1, brief nod to zero enumeration cost! I like this way best. (linq examples aside) :)
Exactly what I was looking for, thank you! I literally just wanted ti figure out how to get the child count, so many people overcomplicating things!
Answer by runevision · Jan 21, 2010 at 10:19 AM
The first piece of example code in the scripting reference for the Transform class shows how to do this:
// Moves all transform children 10 units upwards!
for (var child : Transform in transform) {
child.position += Vector3.up * 10.0;
}
If you want not only the immediate children but also the grandchildren etc., you can use GetComponentsInChildren:
// Rotate all transform children (and their children, etc.)
// by 90 degrees relative to their parents.
var children = GetComponentsInChildren (Transform);
for (var child : Transform in children) {
child.Rotate(0, 90, 0);
}
How do you do this selectively, to only one child, say nested two deep, so it's a grandchild of the object with the script attached.
You can find the "first" child by putting a break; inside the loop - however, it is undefined what the first child is, so this is only reliable if there is only one child. You can find the child of a child by using a nested loop and putting a break; in both the inner and outer loop.
dissidently, there's nothing like xpath for X$$anonymous$$L, if that's what you're looking for. (Though that would be handy -- let us know if you implement it!)
Answer by Ippokratis · May 27, 2011 at 02:30 PM
Try this if other Unity Script solutions fail (as in my case, Unity 3.3 ):
function Start()
{
var myTrans:Transform[] = gameObject.GetComponentsInChildren.<Transform>() as Transform[];
for (var child : Transform in myTrans )
{
child.position = Vector3 (1.0,1.0,1.0);
}
}
Answer by jhon143 · Jan 15, 2021 at 05:34 AM
This is how we can get the child's count of each parent
public List Bottles = new List();
foreach(GameObject GetBottles in Bottles) { Debug.Log("Get Bottles are : " + GetBottles); if (GetBottles.transform.childCount > 1) { Debug.Log(GetBottles + "has Child Count is "+GetBottles.transform.childCount.ToString()); } }