Adding and removing a material at runtime

If you have an object with multiple Materials, that ALSO implies that the mesh has multiple submeshes.

Each Material in the .materials array corresponds to a submesh, so just adding a material to an object isn’t likely to do much.

If you think you still want to, you need to:

  1. create a new blank array of Materials of the right size (+1 or -1 in size)
  2. copy the existing Materials out to the correct spot in this temp array
  3. add (or omit) the newly added (or removed) material.
  4. assign the array of Materials into the .materials property all at once.

You can NOT simply assign one element of the .materials array. You must at least pluck the whole thing out, change it, then also put it back. The reason: accessing it causes Unity to make you a copy.

EDIT: The same goes for .material: that returns a copy (as per the docs). You can copy it out, change it, but you MUST assign it back in.