- Home /
How to make only the parent gameobject invisible ?
Hi all, In my Unity Scene I have a Gameobject which contains a lot of children Gameobjects. Now if a certain trigger is set I would like to make only the parent invisible so that the children still exists in the scene (so not disable the parent) . I tried to do youbot.GetComponent().enabled = false; but it seems that my gameobject ( the youbot) dont have a renderer but one of the child has, so the parent gameobject is still visible. Can someone suggest a solution to this? Hope for your help and many thanks !
Can you post a screenshot of the hierarchy and the inspector of the game object?
Answer by pako · Sep 27, 2018 at 08:30 AM
@neymar4138 what you are saying doesn't make sense per se: If the parent GameObject doesn't have a renderer, it's not visible anyway, since it's the renderer component that makes something visible in the scene in the first place.
In general, if you want to make something invisible, you must access its renderer component and disable it. So, in your case, you need to find which renderer is rendering what represents the parent GameObject (maybe a child of the parent), access its renderer, and disable it.
Answer by SaiKrishna149 · Sep 27, 2018 at 08:22 AM
void OnTriggerEnter(Collider other) { for (i=0; i<= transform.childcount; i++) { transform.GetChild (i).gameObject.SetActive (false); }
}
That would actually do the opposite. It would disable all children.
access parent with name and turn off the mesh renderer
@Sai$$anonymous$$rishna149 the OP has already said that the parent does not have a renderer.