0
\$\begingroup\$

In Unity, I created an empty GameObject called "Person". I created some child objects to it, such as "head" (a sphere), "body" (a capsule), "leg" (a box), etc. Then, I wanted to change the person's color, so I created a material. However, I could not drag & drop it on the person - I could only drag and drop it on each child object separately.

My question is: what game objects in Unity are allowed to have a "material" on them? And, is there a way I can put a material on a parent object, and let it affect all its child and descendant objects?

\$\endgroup\$

2 Answers 2

4
\$\begingroup\$

There are actually two completely unrelated kinds of materials in Unity: Renderer materials (which define how things are rendered) and physics materials (which define how rigidbodies behave when they bump into each other). But if you are talking about color, you are likely talking about renderer materials.

Generally speaking, you can assign a material to any game object which has a Renderer component. There are lots of different kinds of renderers. MeshRenderers for 3d models, SpriteRenderers for sprites and a couple specialized ones like LineRenderer or TrailRenderer. But one thing all renderers have in common is that they all use materials (in some cases more than one, for example when you have a 3d model which uses multiple materials then the MeshRenderer will have a separate material slot for each of them).

It is possible that your "Person" is just a hierarchical object which has no renderer itself but has several children which do. In that case you will have to assign the material to each of the renderers of the children.

If you want to do that from the editor, then you will have to select each of the children and assign the material to their respective renderers.

If you want to do that from a script, you can get an array with all the renderers in all the children using GetComponentsInChildren<Renderer>(). It returns an array of Renderer interfaces. This interface represents the lowest common denominator of the functionality of all the specialized renderers and only exposes those functionality they all have in common. One of those is renderer.material.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks! What confused me is that, in the Unity inspector, the Material looks like a separate component, while in fact it is a field of the Renderer component. \$\endgroup\$ Commented Jan 28, 2020 at 16:09
1
\$\begingroup\$

Sounds like you have combined multiple surfaces together to create one 'person' (object), and the material is attached to that singular surface. So if you wanted to apply one Material to that person as a whole color then it would need to be a singular surface (to my understanding at least).

Reference: https://docs.unity3d.com/Manual/Shaders.html

In Unreal you could attach that material to the parent object and its sub objects would inherit that surface, but to my knowledge that is still not a feature in unity yet. You would need to do this programatically if you wanted it to iterate through a list of sub-objects (or do it manually) like they did here: https://answers.unity.com/questions/56819/assign-material-to-multiple-objects.html

Hopefully this helps.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .