- Home /
GetComponentsInChilden
Hello, I need some help with Javascript coding. Basically I have a parent object that I have attached other objects to and made them children of it. They are all instanced objects that have code attached to them and I am trying to figure out how to use GetComponentsInChildren to find the script in all the children and run it for each of them but I guess i'm not understanding how to use GetComponentsInChildren in relation with scripts. If anyone can help me understand how to use GetComponentsInChildren to find scripts in children and then execute a function in all of those scripts it would be greatly appreciated.
Answer by Eric5h5 · Jul 24, 2011 at 12:55 AM
var scripts = GetComponentsInChildren.<MyAwesomeScript>();
for (script in scripts) {
script.DoSomething();
}
Answer by Cyberkada · Nov 07, 2011 at 03:01 PM
var name = GetComponentsInChildren(hypRegistry);
for (var nameSet : hypRegistry in name)
{
nameSet.setTexture(shipNumber - 1);
}
Javascript version.
Just for your information: Eric's answer is in UnityScript (Javascript). btw: you should overthink your variable and class names, they are kinda confusing. Classnames should start with a capital letter.
Also, I wouldn't recommend this method, since GetComponentsInChildren(ScriptName)
returns Component[], whereas GetComponentsInChildren.<ScriptName>()
returns ScriptName[]. So you don't have to deal with dynamic typing.
Your answer
Follow this Question
Related Questions
InvalidCastException in Javascript 2 Answers
Setting Scroll View Width GUILayout 1 Answer
Changing the renderer.material.color of the children OnCollisionEnter? 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Audio play problem on raycast hit colider.gameObject.tag 1 Answer