turn off parent Gameobject based on a child game object that entered a Trigger?
hey! so i gave this pice of code:
public GameObject player;
void OnTriggerExit (Collider myTrigger) {
if(myTrigger.gameObject.tag == "Player"){
Debug.Log("Box went through!");
myTrigger.gameObject.SetActive(false);
isPlayerVisible = false;
}
}
that works pretty fine, i get the debug log and so on. what i want to do now is, that the Gameobject that should gets deactivated, is a Parent of the Trigger. so the Hierarchie is:
-MainGO
--CharacterController
---myTrigger(the GO that gets turned off at the moment)
i want to turn off the MainGO
Answer by antx · Jan 25, 2016 at 05:02 PM
myTrigger.transform.parent.parent.gameObject.SetActive(false); // should do the trick.
With .transform you get your triggers transform component, with .parent you get the parent transform of that (which would be CharacterController), with the second .parent you get the parent transform of that transform (which is MainGO) and with .gameObject you have the GameObject of it on which you can use the SetActive(false);.
Answer by ShadyProductions · Jan 25, 2016 at 04:58 PM
myTrigger.transform.parent.parent.gameObject.SetActive(false); // I think :P
//myTrigger.transform.root.SetActive(false); //could work too, it returns the highest in hierarchy.
Close, but you need to use "parent" of the Transform since GameObjects have no parent property.
Your answer
Follow this Question
Related Questions
Gameobject doesnt move after build 0 Answers
Can’t keep the position 0 Answers
Can not access GetComponentInParent ( ). usegravity 1 Answer
NullReferenceException when trying to access parent 0 Answers