How To Deactivate A Parent (And Its Children)?

Hello,

I have an object with a load of children - How do I deactivate the whole object (including children) so It's not visible during the game via JavaScript?

Also, is there a performance difference between deactivating renderer and deactivating object?

Thanks, Ollie

From Scripting Docs:

  • function SetActiveRecursively (state : bool) : void

    `Sets the active state of this and all the game objects children to state.`

Thus to deactivate Object and all its children:

var ObjectToDeactivate = GameObject.Find("GameObjectName");
ObjectToDeactivate.SetActiveRecursively(false);

Since None of the 'Find' commands see deactivated objects, I am not sure if a reference to the active object(parent) saved in a static variable prior to deactivation will allow `ObjectToDeactivate.SetActiveRecursively(true);` to reverse the process.

UPDATE: yes, the object is still in memory... I tested a static var, it works. Although I would prefer not to have to stack memory with static vars, I see no other way to access the object... as of now.

I've found out how:

var renderers = GetComponentsInChildren(Renderer);
for (var r : Renderer in renderers) {
    r.enabled = false;
}

Children are stored in the Transform of their parent. In order to deactivate all the children you have to loop through them like so:

for(var child : Transform in parent)
          child.gameObject.active = !child.gameObject.active;

I'm not sure about the performance difference, but I would guess that at least from the rendering perspective, there is no difference between deactivating the renderer or the whole object.

From what I see, de-activating has better performance than de-rendering, assuming this is correct English.

for example : performance between disable renderer and disable gameObject - Questions & Answers - Unity Discussions