- Home /
Gameobject with many child objects disable mesh not working.
Hi guys,
Now this is already a discussed topic and I still did not get how to solve the issue.
Problem:
I have an architectural building with many child game objects in it. Some having renderer and some gameobjects(without renderer) having further child objects which in return has more children. So I want to make the whole building invisble in a click of Button1 and make it visible again on clicking Button2. I have written the following code but it does not works.
using UnityEngine; using System.Collections;
public class ActionScriptChangeVisibility: MonoBehaviour
{
public Renderer[] objectHavingChild;
public void InvisibleBeacon() //Button1
{
objectHavingChild = GetComponentsInChildren<Renderer>();
foreach (Renderer rend in objectHavingChild)
rend.enabled = false;
}
public void VisibleBeacon() //Button2
{
objectHavingChild = GetComponentsInChildren<Renderer>();
foreach (Renderer rend in objectHavingChild)
rend.enabled = true;
}
}
Question: This script is attached to the buttons. I don't get how to disable a particular parent game object along with child. How do I make the whole building invisible? In this code there is no option to select the particular gameobject to make it invisble. Any help is appreciated.
Thanks in Advance.
Attach the script component to the root GameObject of your building ins$$anonymous$$d of your button and reference it with the button' OnClick action (assu$$anonymous$$g you are using the new Unity UI: http://docs.unity3d.com/$$anonymous$$anual/script-Button.html).
And just a tip: I think you will get the same behavior with much less hassle if you deactivate the root GameObject (root.SetActive(false)) ins$$anonymous$$d of all children's renderer components?
Your answer
![](https://koobas.hobune.stream/wayback/20220612062053im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
InvalidCastException when trying to access child components 2 Answers
Visibility based on Tags 2 Answers
Is it possible to start a game and get GUI-button act as like its already pressed? (Javascript) 1 Answer
Cannot cast form source type to destination type? 2 Answers
renderer.enable = photon? 0 Answers