- Home /
 
               Question by 
               DrVonTrap 1 · Mar 24, 2011 at 05:16 PM · 
                componentchildren  
              
 
              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
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by Jessy · Mar 24, 2011 at 06:23 PM
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);
}
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                