How to change the color of a game object (hierarchy of Unity basic shapes)

I have a temporary “eneny” that is make up of basic shapes. To change the color I use the code below.

selectedTarget.renderer.material.color = Color.red;

However, this just changes the color of his head which happens to te the top shape in the hierarchy. How can I change just the body, feet, or all the shapes that make up the “enemy”?

You’ll need to go through thte hierarchy and do the same with each renderer. otherwise you could instance a copy of the material and assign it to all the parts using .sharedMaterial at which point you can edit the material to reflect the change everywhere its shared.

.sharedMaterial makes the entire game the same color!! I do not see a way to step though the hierarchy. Where do I do that?

Can you do something like…

At top of Class define an array to hold the renderers
Component[ ] renderers;

In Start() Cache the renderers
renderers = gameObject.GetComponentsInChildren(typeof(Renderer));

Then, when you want to change the colors:
foreach (Renderer renderer in renderers)
{
renderer.material.color = Color.red;
}

This is C# and ‘off the top of my head’ without testing, but it may get you moving in a direction.

Regards,
Matt.