- Home /
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
Answer by oliver-jones · Dec 01, 2010 at 11:15 PM
I've found out how:
var renderers = GetComponentsInChildren(Renderer);
for (var r : Renderer in renderers) {
r.enabled = false;
}
answering you own question is perfectly legitimate, but please mark it as answered.
I know - I have to wait 42 hours until I can tick it, otherwise I would :)
Answer by dingben · Dec 05, 2010 at 12:13 PM
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.
hello guys, is there a way to do the same in c sharp. Cos AFAI$$anonymous$$ c sharp doesnt support static variable..help me out folks
@elixir.bash you can have static variables with C# though there are some strong motivations to avoid static variables unless absolutely necessary. http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.80).aspx
Answer by ericksson · Dec 01, 2010 at 09:28 PM
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.
I'm getting error with the 'in parent', do I need to change that to a name of my object?
Yes, parent is a variable that stores a reference to the object you wish to disable along with its children.
Answer by jonc113 · Apr 17, 2013 at 03:44 PM
From what I see, de-activating has better performance than de-rendering, assuming this is correct English.
for example : http://answers.unity3d.com/questions/217968/performance-between-disable-renderer-and-disable-g.html#answer-440001
Objects which are not visible should be frustum culled meaning that they will not get rendered since they are out of view. The Pro version of Unity also offers occlusion culling which is even more effective in many circumstances.
Your answer
Follow this Question
Related Questions
enable/disable child objects? 2 Answers
How to activate another Object with java script? 4 Answers
Is there any command that can reference deactivated objects? 1 Answer
turn on a game object? 1 Answer
C# Find component InChildren 3 Answers