0
\$\begingroup\$

I'm trying to change the color/material of the faces on my object at runtime. My object has a default SpatialMappingWireframe to begin with:

enter image description here

And I'm trying to assign new colors on start like so:

void Start()
{
    Mesh mesh = GetComponent<MeshFilter>().mesh;

    Color[] colours = new Color[mesh.vertices.Length];

    for (int i = 0; i < mesh.vertices.Length; i++)
    {
        colours[i] = Color.red;
    }

    mesh.colors = colours;

    Debug.Log($"Colour Count: {colours.Length}");
}

I can see the debug line being output, but no changes to my scene:

enter image description here

Apologies if I'm missing something obvious

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

The default shader on a material does not support assigning new colours. Assigning a new shader to the material asset resolves the issue:

Quoting the a StackOverflow answer:

  1. in the Project view (Assets) do right clickCreateMaterial

    enter image description here

  2. Give that material a name

  3. For the Shader from the Dropdown menu find and select e.g. ParticlesStandard Unlit (or if you want to receive lightning Standard Surface)

    enter image description here

  4. Finally use this material for your object either by dragging it into the MeshRenderermaterial or by simply dragging it onto the according object in the Scene view (the latter might not work if there is no mesh for that object yet)


Result:

enter image description here

\$\endgroup\$

You must log in to answer this question.

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