0
\$\begingroup\$

I have a main_mesh that has 10 submeshes, I wonder how I can change the color of of these submeshes to a different color (e.g submesh1 will have a red color, submesh2 will have a blue color,...etc). Any advise please?

UPDATE:

This is how I'm getting my mesh which has 10 submeshes:

        SkinnedMeshRenderer smr  = gameobject1.GetComponent<SkinnedMeshRenderer>();
        Mesh main_mesh = smr.sharedMesh;
\$\endgroup\$
1
\$\begingroup\$

Usually , you have a different material for each submesh. If this is the case change the properties of each material to achieve what you want

Example: I tried this

Renderer r =  GetComponent<Renderer>();
    r.materials[0].SetColor ("_Color",new Color(123f/255f, 0f/255f, 123f/255f));

Where r,g,b are your color components.

Some references : Ref1 , Ref2

\$\endgroup\$
5
  • \$\begingroup\$ Thanks for your answer but I'm asking how I can implement this? Like if my mesh is named 'mesh' \$\endgroup\$ – Tak May 29 '15 at 9:50
  • \$\begingroup\$ your code gives this error Error CS1061: 'UnityEngine.Component' does not contain a definition for 'materials' and no extension method 'materials' accepting a first argument of type 'UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?) (CS1061) (Assembly-CSharp) \$\endgroup\$ – Tak May 29 '15 at 11:18
  • \$\begingroup\$ used : renderer.materials ? The gameobject contains a renderer? \$\endgroup\$ – dnk drone.vs.drones May 29 '15 at 11:27
  • \$\begingroup\$ you can use : renderer = GetComponent<Renderer>(); //to get current mesh renderer \$\endgroup\$ – dnk drone.vs.drones May 29 '15 at 11:36
  • \$\begingroup\$ could you please check my updated question, I showed how I got my mesh \$\endgroup\$ – Tak Jun 2 '15 at 2:37
1
\$\begingroup\$

If you have N submeshes, the renderer for those meshes will need to have N materials.

Many tutorials focus on renderers with only one material, which you access through its material property. If you have multiple materials, you can instead access the materials array. Each material n corresponds to submesh n.

If you want, you can apply different materials to each submesh by editing your assets in the inspector. Each material could have different textures, different instance parameters, or so on.

Or, you could apply one material to all of them, and then edit that material in a script. Bear in mind that Unity uses a copy-on-access pattern, so it will internally create N new material instances the first time you access the materials array.

As an example, the following would apply a random color to each submesh:

void Start() {
    Material[] materials = renderer.materials;
    for (int i=0; i<materials.Length; i++) {
        float r = Random.value;
        float g = Random.value;
        float b = Random.value;
        materials[i].color = new Color(r, g, b, 1f);
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ could you please check my updated question, I showed how I got my mesh \$\endgroup\$ – Tak Jun 2 '15 at 2:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.