• 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
6
Question by davedev · Mar 09, 2010 at 10:59 AM · gameobject

How can I enable and disable a complex gameobject?

I created a empty gameobject and added a model to it. How can I disable the entire game object including its child model? I tried active and enabled on the root gameobject, but it didn't seem to have any effect. I want to hide the model until it is needed.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by fafase · Jan 10, 2013 at 12:19 AM

Little Update:

As for Unity4 SetActiveRecursively is outdated and replaced by

 obj.SetActive(true);

http://docs.unity3d.com/Documentation/ScriptReference/GameObject.SetActive.html

If you still use a previous version, refer to the other answer.

Comment
Add comment · 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
17

Answer by duck · Mar 09, 2010 at 11:36 AM

GameObject.SetActiveRecursively might be what you're looking for.

However, if you only want to hide the model, rather than deactivate it, you could always just turn off the Renderer of the child object(s). This would mean your GameObject's scripts would still receive Update events and other Unity events.

If there are many child objects, and you want to turn the renderer off on all of them, you could use this. This would also work for a single child object.

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

If you want to turn on/off only specific children of an object, based on a particular tag, you can just check the ".tag" property of the renderer reference (this gives you the tag of the GameObject to which the renderer is attached). For example, this would turn off the renderer of all child gameobjects tagged "JetGlow":

var renderers = GetComponentsInChildren(Renderer);
for (var r : Renderer in renderers) {
    if (r.CompareTag("JetGlow")) {
        r.enabled = false;
    }
}
Comment
Add comment · Show 7 · 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 11:44 PM 0
Share

Is there anyway to 'hide' specific children? Through name, or tagging?

avatar image duck ♦♦ · Dec 02, 2010 at 07:28 AM 1
Share

Yes - I have just updated the answer to include an example of this.

avatar image MacSecrest · Jan 09, 2013 at 11:55 PM 0
Share

Could you rewrite the tag specific one in C#?

avatar image Bunny83 · Jan 10, 2013 at 12:04 AM 0
Share

@$$anonymous$$acSecrest:
Here's the C# version:

 //C#
 var renderers = GetComponentsInChildren<Renderer>();
 foreach(var r in renderers) {
     if (r.CompareTag("JetGlow")) {
         r.enabled = false;
     }
 }
avatar image Bunny83 · Jan 10, 2013 at 12:08 AM 0
Share

Btw, keep in $$anonymous$$d that GetComponentsInChildren usually just return active / enabled components.

Unity has added an overloaded method which takes a boolean to specify if you want to include inactive objects as well.

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Animation issue (OnTriggerEnter) ***SOLVED*** 1 Answer

If i disable a GameObject does all it's components get disabled also? 2 Answers

I'm trying to convert a Transform to a GameObject (?) 3 Answers

Character moving weirdly 1 Answer


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