I have some game objects in my scene that have a mesh renderer attached where in the materials list there is the default material attached plus one empty slot free:
Element 1 is empty so I can add my custom shader material at runtime with code:
var mats = GetComponent<MeshRenderer>().materials;
mats[1] = material;
GetComponent<MeshRenderer>().materials = mats;
This element 1 material slow get added and removed depending if the player has clicked on the object to make a nice outline effect for selected object.
This is one game object without the element 1 material attached.
Looks just normal.
Now when I click on it it adds the shader material and then it looks like this:
However when I build the project all game objects that have the option of having the shader material additionally added to the materials list lose all color:
But that should not be a problem according to the warning.
“This is only a performance notification not a real problem. The problem is that when your mesh has more than 65534 vertices, your mesh is automatically divided into sub-meshes. In your case if you have 1 mesh with more than 1 material, it is multiplied by sub-mesh divisions, and your performance will be affected because each material will result in a draw-call.”
At the least I would not leave the second material unassigned. Perhaps this is causing an issue and it might just go away if you assign “Lit” to the second material as well.
There’s also a possibility of using this code on a MeshRenderer which only has one material. Check if materials actually has a length of 2 or more.
Then we might just have one of these cases where sharedMaterials ought to be used rather than materials to avoid making duplicates.
Lastly, I wouldn’t re-assign the entire materials array (this might run additional code and could also be responsible for things breaking) when you can do: GetComponent<MeshRenderer>().materials[1] = material;
tried that also
no joke tried all the things you said already.
i also found this:
also tried: “Go Settings → Graphics, and add the shaders you need to the “Always Included Shaders” list.”
and also tried many more things. after around 5 hours i gave up and are now working on a different solution
dont want to waste more time if i can use a different solution thats 75% as good looking.
Nope, that would not work. The materials array is a “copy” of the materials in use. You do not get direct access to the materials array as they are stored on the native side. You have to reassign the materials array to actually cause unity to use the new materials.
Though I agree that you should not have an unassigned material slot. If you need to switch the materials often, you probably want to store two materials arrays which you switch out. One with just one material and another one with two.