• 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
Question by jillytot · Nov 07, 2013 at 10:44 PM · childrendererdisable

Disable renderer of a child

Works for the parent, but the child is still visible. They both have a mesh renderer applied.

Any ideas?

 renderer.enabled = false;
     Renderer renderChild = GetComponentInChildren<Renderer>();
     renderChild.enabled = false;

Thanks in advance!

Comment
Catherine-Cail
robomarti

People who like this

2 Show 0
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

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by chaosmaker · Nov 07, 2013 at 11:31 PM

Here's the solution:

 foreach (Renderer r in GetComponentsInChildren<Renderer>())
   r.enabled = false;



The problem here lays in the documentation of Component.GetComponentInChildren:

"Returns the component of Type type in the GameObject or any of its children using depth first search."

So here it returns the component in the current (parent) GameObject itself, not the child one.

To check this:

 Renderer renderChild = GetComponentInChildren<Renderer>();
 Debug.Log("RenderChild: " + (renderChild == null)); // First check if it has a valid reference
 Debug.Log("Name: " + (renderChild.gameObject.name)); // Will return your parent object's name, not the child one

So solution is getting the all Renderer components of parent and children, then iterate through them and disable them as follow:

   Renderer[] renderers = GetComponentsInChildren<Renderer>();
   foreach (Renderer r in renderers)
       r.enabled = false;

or simply

 foreach (Renderer r in GetComponentsInChildren<Renderer>())
   r.enabled = false;
Comment
vexe
Josh707
Fattie
basti12354
Catherine-Cail
robomarti

People who like this

6 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 jillytot · Nov 07, 2013 at 11:59 PM 0
Share

This totally worked. I guess it wasn't clear on the documentation that i had to use an array since i only had one child.

avatar image

Answer by Lovelock · Nov 07, 2013 at 11:16 PM

Perhaps you are looking for

 gameObject.SetActive(false); // Disables game object and children recursively

This will disable the game object's rendering and all children. If you are looking to disable just the renderer, try this:

 Renderer[] renderChildren = GetComponentsInChildren<Renderer>();
 
 int i = 0;
 for(i = 0; i < renderChildren.Length; ++i)
 {
     renderChildren[i].renderer.enabled = false;
 }
Comment

People who like this

0 Show 0 · 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

Answer by Ali-Nagori · Nov 07, 2013 at 11:51 PM

try this one

 renderer.enabled = false;
 Transform father = transform;
 foreach(Transform child in transform)
 {
     child.transform.renderer.enabled = false;
 }
Comment

People who like this

0 Show 0 · 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

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Disabling/Enabling components in children of children? 1 Answer

change child texture problem 2 Answers

Make a simple tree 1 Answer

Disable multiple child components 1 Answer

Disabling all scripts on child only? 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