- Home /
How can i get all childs from List ?
List<GameObject> cubes = new List<GameObject>();
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.
Answer by YakiC · Apr 02, 2017 at 10:12 AM
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.
Answer by toddisarockstar · Apr 02, 2017 at 06:30 PM
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[i].transform) {
kids.gameObject.AddComponent<nameofscript> ();
}
}
Answer by joelplourde · Apr 02, 2017 at 09:33 PM
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[i].ChildCount; j++)
{
//Add a component to the Child of the current Element in the Cubes List.
Cubes[i].GetChild(j).gameObject.AddComponent<mouseDrag>();
}
}
So since I am new to this Unity Formating and all, I'd explain down here:
For each elements in the List.
For each child of the GameObject present in the list.
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
Your answer
![](https://koobas.hobune.stream/wayback/20220612114454im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why when creating new animator controller for the character the character is not walking right ? 0 Answers
How can i Instantiate on the terrain from left to right ? 0 Answers
Why the tile map scripts take almost all the cpu usage ? cpu usage is getting to 99% at times 1 Answer
How can i rotate object by pressing on key R and keep object facing to me my self ? 0 Answers
How can i spawn new gameobjects to be inside the terrain area ? 2 Answers