- Home /
GetComponent in Multiple Children
Hi, I have been working on a script in which at the moment taht a player passes thorugh a trigger it activates its weapons, but the problem that I am having is that is activating only one of four weapons that are exactly identical. I tried with GetComponentsInChildren but it send a compiler error. How could I make it so that it activates all of the children?
Trigger Script: #pragma strict
function OnTriggerEnter(col : Collider)
{
if (col.gameObject.name == "Minigun_Trigger")
{
GetComponentInChildren(Gun_Script).weaponUnlocked = true;
GetComponentInChildren(RocketLauncher).rocketsUnlocked = false;
GetComponentInChildren(SmokeCreator).smokeUnlocked = false;
yield WaitForSeconds(40);
GetComponentInChildren(Gun_Script).weaponUnlocked = false;
}
}
use GetComponentsInChildren.
GetComponentsInChildren will return an array of component scripts and then iterate through that.
for(var gs:Gun_Script in GetComponentInChildren(Gun_Script)) gs.weaponUnlocked = true;
Thanks, it is working, for next time you should post it as an answer so that I can mark it as the correct answer.
Answer by Geo.Ego · Jan 14, 2013 at 10:25 PM
I'm putting in tomekkie2's comment as an answer here so that others can see the solution more easily:
Use GetComponentsInChildren ().
GetComponentsInChildren will return an array of component scripts and then iterate through that.
for (var gs:Gun_Script in GetComponentInChildren(Gun_Script))
{
gs.weaponUnlocked = true;
}
@tomekkie2: if you'd like to submit this as an answer yourself, Orloffyeah can mark it as the answer instead; it's just far more useful for a question to have an accepted answer so people can find it and Orloffyeah's accept rate isn't negatively impacted.
Geo.Ego: If you have the karma, there should be a round button with a curved arrow in it, next to Delete and Upvote, which is 'convert comment to answer' which would take care of this for you.
I must not have enough karma yet. $$anonymous$$y only option is to upvote.