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?

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"!

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);
}

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);
		}
	}
}

voxelform.com

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);
	}
		
}

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());
}
}