- Home /
Child object of another GameObject invisible or inactive @ runtime
How do you disable the render or inactivity of the child object of another object?
Answer by Sebas · Dec 27, 2010 at 10:17 AM
You can set the active state of any GameObject and all of its children with SetActiveRecursively You can also find individual children by name like this and disable their renderer individually with
// make the object invisible
renderer.enabled = false;
Beat me to it by 5 seconds, good job. You should also add that because it only searches in the GO's children it also has the benefit of not slowing everything down to a crawl like Find usually does.
does setting an object to be inactive also removes its render?
The renderer itself is never removed unless you specifically remove the component. Deactivating a game object will result in the object not being rendered anymore.
Answer by SkyCharger001 · May 11, 2018 at 09:30 AM
Parentobject.transform.localScale = new Vector3(0,0,0)
affects all its children without needing to traverse anything and it allows one to have multiple invisibility-chains without the risk of loosing any of them when one is undone.
Even though it works, I would really recommend against it. Changing the scale will make all the children recalculate their transforms and their physics, which can be very costly, and they would still be rendered. This means that if you have a special shader, it might not even disable its rendering. Not to mention that all the other scripts would still run.
It is a lot easier to just deactivate the game object or its renderer. Even though SetActiveRecursively()
was deprecated in Unity 4 and does not exist anymore, it is pretty easy to write a similar function.
but as far as i know this is the only approach that can be used if you have multiple invisibility-chains that could otherwise corrupt each other. (EG: disable "Floor[5]/room[9]" during chapter 2 and disable "Floor[5]"during chapter 3: if one used something like SetActiveRecursively() then undoing the latter would undo the former as well, wether it's desired or not.)
I see what you mean, and probably this is why SetActiveRecusively()
was removed: a deactivated parent hides all the children as well, so SetActiveRecusively()
is unnecessary, in fact, it causes more problems. You can easily achieve the above with calls to SetActive()
:
- in chapter 2, call SetActive(false)
on "Floor[5]/room[9]" to disable it; Floor[5] remains, but room[9] gets hidden
- in chapter 3, call SetActive(false)
on "Floor[5]" - the whole Floor[5] will get disabled, including room[9] (which would still be disabled if you enable Floor[5]
Does this make sense, or am I still not seeing the issue?