So Unity is failing to assign the requested material. Its somehow loading a completely different “default” material.
I am able to assign the OutlinedMaterial_Friendly to Material slot 2 in the inspector, however, this is insufficient for my purposes. I need to be able to change this slot programmatically.
Any ideas on what I am doing incorrectly? Or is this a bug in Unity? Thanks
You can’t update the array of materials directly. You need to take a copy of it, update the position you want to change and then update the whole array of materials. I.e.:
Note: This looks like the expected behaviour, from the manual: “Note that like all arrays returned by Unity, this returns a copy of materials array. If you want to change some materials in it, get the value, change an entry and set materials back.” - Unity - Scripting API: Renderer.materials
you can set more than one materials from scripts only if you have more than one slots of material in inspector, you had to manually describe how many materials you want on your model (they should be equal to the number of UV sets your model has) … Go to INSPECTOR > MESH RENDERER >MATERIALS and increase SIZE field
I know the thread is a bit old but in case others come across this issue in 2022 +
@DSebJ 's answer above got me to my solution. I expect most people looking for this are trying to add an outline material to a character model (or similar 3D model).
you must have at least 2 materials on a mesh renderer or skinned mesh renderer component.
this script achieves replacing the mesh renderer’s materials array when the mouse hovers over it. attach this script to the parent object of the 3d model in question.
Drag the 2 materials into the array slots in the inspector ( the ones that will replace the original ones when you hover over the object)
remember you are replacing the whole array not just 1 material.