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!

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;

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*.renderer.enabled = false;*

}

try this one

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