- Home /
The question is answered, right answer was accepted
HIDE CHILD script - help
what i need to happen: when the player collides with trigger and "B" is pressed the child of that trigger hides.
what does happen: i get an error saying " unknown identifier "child""
here is my script;
function OnTriggerStay(trigger : Collider) {
if((trigger.gameObject.tag == "Player") && Input.GetKeyDown(KeyCode.B)) {
child.renderer.enabled = false;
}
}
any help would be great
thanks
Answer by Phantomized · Oct 20, 2014 at 06:32 PM
Basically you need to get the child transform(s) first before you can tell its renderer to stop rendering. Here are 2 ways:
If you only have one child to make invisible:
function OnTriggerStay(trigger : Collider) {
if((trigger.gameObject.tag == "Player") && Input.GetKeyDown(KeyCode.B)) {
var child = transform.GetChild(0);
child.renderer.enabled = false;
}
}
If you want to make all the children invisible:
function OnTriggerStay(trigger : Collider) {
if((trigger.gameObject.tag == "Player") && Input.GetKeyDown(KeyCode.B)) {
for (var child : Transform in transform) {
child.renderer.enabled = false;
}
}
}
Answer by RayJr · Oct 20, 2014 at 03:03 PM
Sorry, that was a lame response :P
Basically, when you hit the trigger, you need to go get the child/childern then you can do stuff to them.
this post explains how to get the childern from the parent.
http://answers.unity3d.com/questions/689549/find-child-objects-by-name.html
Answer by spiceboy9994 · Oct 20, 2014 at 04:14 PM
Not sure that actually Child exist. Have you tried Transform.GetChild?
http://docs.unity3d.com/ScriptReference/Transform.GetChild.html
Another thing that may help you is try to print the name (or the tag) of the element that is triggering the function, just to be sure that the object has a rendered attached to it.
Regards