• 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

  • 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

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

make an object disapear? 2 Answers

Deactivate Specific Children In Partent 1 Answer

Access the renderer of a clones child object 1 Answer

Are disabled objects rendered/calculated? 1 Answer

Disable an child object 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