Add script to many multiple children

Hey, i have one control object, that has over 150 children, each child is very similar. I need a way to add the same script to each of the child objects; either in the editor or with scripting on runtime. i thought about using `gameObject.AddComponent()` but i've found no way to make that effect every child object.

thanks in advance

1 Answer

1

Iterate through the children with a loop, demonstrated in the code sample on the Transform page. If you need to run the code in the Editor, a context menu might be appropriate.

@ ContextMenu ("Add cool script to all children") function AddCoolScriptToAllChidlren () {
    for (var child in transform) child.AddComponent(CoolScript);
}