- Home /
Need help with get child component script
not sure how to script it, but its attached to the end of my player movement script and when i run over the box collider it enables new weapon and disables old weapon.
function OnTriggerEnter (other : Collider)
{
if ("Weapon" + tag);
{
other.gameObject.SetActive (false);
GetComponentInChildren("Mace").enable = true;
GetComponentInChildren("Sword").enable = false;
}
}
Answer by Regalith · Dec 30, 2014 at 04:08 AM
The if statement is wrong you need a comparison inside the parenthesis. Here's possibly what you need. This is assuming you have tags "Mace" and "Sword" on different pick ups.
function OnTriggerEnter (other : Collider)
{
//Turn of the mace when running over the mace pickup
if (other.transform.tag == "Mace");
{
other.gameObject.SetActive (false);
this.GetComponentInChildren("Mace").enabled = true;
this.GetComponentInChildren("Sword").enabled = false;
}
}
I keep getting this error: Assets/AScripts/Player1.js(41,44): BCE0023: No appropriate version of 'UnityEngine.Component.GetComponentInChildren' for the argument list '(String)' was found.
That's because there is no GetComponentInChildren takes a type not a string. didn't realize the error since i copied your code inside the if statement. GetComponentInChildren only works on components of the children. If you have a gameobject mace that you want to turn on, your best bet is to either use Traform.Find and find the name, or drag it into a public variable in the script.