Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by oliver-jones · Dec 01, 2010 at 08:07 PM · objectrendererchildrenenableactivate

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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;
}
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rennat · Dec 02, 2010 at 05:56 AM 0
Share

answering you own question is perfectly legitimate, but please mark it as answered.

avatar image oliver-jones · Dec 02, 2010 at 06:45 AM 0
Share

I know - I have to wait 42 hours until I can tick it, otherwise I would :)

avatar image
8

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fireDude67 · Jan 26, 2011 at 02:54 AM 0
Share
  • for SetActiveRecursively()

avatar image numberkruncher · Feb 18, 2012 at 05:38 PM 0
Share
  • Cheers!

avatar image davvilla · May 30, 2012 at 07:10 PM 0
Share

Perfect, thanks !

avatar image elixir-bash · Aug 30, 2012 at 06:06 AM 0
Share

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

avatar image numberkruncher · Aug 30, 2012 at 02:16 PM 0
Share

@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

Show more comments
avatar image
1

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image oliver-jones · Dec 01, 2010 at 09:45 PM 0
Share

I'm getting error with the 'in parent', do I need to change that to a name of my object?

avatar image oliver-jones · Dec 01, 2010 at 10:31 PM 0
Share

Yeah - thats not working ...

avatar image ericksson · Dec 03, 2010 at 11:42 AM 0
Share

Yes, parent is a variable that stores a reference to the object you wish to disable along with its children.

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image numberkruncher · Apr 17, 2013 at 04:54 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges