How can i get all childs from List ?

List cubes = new List();

For example i have in the List 10 gameobjects.
Now i want to loop over the gameobjects in the List and to attach to each gameobject child a script. ! Not the the gameobject but to it’s child !

var childs = GetComponentInChildren(, true);

        foreach (GameObject go in cubes)
        {
               
            go.AddComponent<mouseDrag>();
        }

The foreach will add the script to the gameobject but i want to add it to the child of each gameobject. I tried to create a var childs but not sure how to continue.

Hello, assuming you know the name of the child, you can try Transform.Find to get your child then AddComponent

    List<GameObject> cubes = new List<GameObject>();
    foreach (GameObject go in cubes)
    {
        GameObject child = go.transform.Find("NameOfChild").gameObject;
        child.AddComponent<mouseDrag>();
    }

not tested but I’d do something like this if the script can’t be added another way.

this will find EVERY child in every gameobject if there are multiple children in each object;

	int i;	

i = cubes.Count;
		while (i>0) {i--;
						foreach (Transform kids in cubes*.transform) {*
  •  						kids.gameObject.AddComponent<nameofscript> ();*
    
  •  				}*
    
  •  		}*
    

Hello !

As a C programmer, I hate the For Each` Loop, I’d rather complicate things a bit, but have more control over the outcome.

So let me rephrase your question, you got a List of Gameobject, you want to iterate over each of the element in the list, and to attach a script to each CHILD of the Gameobject present in the list ?

//For each Elements in the Cubes List
for (int i = 0; i < Cubes.Count; i++)
{
    //For each child of the elements in the Cubes list.
    for (int j = 0; j < Cubes*.ChildCount; j++)*

{
//Add a component to the Child of the current Element in the Cubes List.
Cubes*.GetChild(j).gameObject.AddComponent();*
}
}
So since I am new to this Unity Formating and all, I’d explain down here:
1. For each elements in the List.
2. For each child of the GameObject present in the list.
3. For each child, add a “mouseDrag” component to it.
Hopefully @Chocolade, I answered your question correctly, if not, well I didn’t understood your question correctly.
PS: .gameObject. may be redundant seeing as you iterate over GameObject already, but I didn’t want to assume the of the Child of the GameObject