I simply need to swap a material on an object with multiple materials. If I run a for loop like so:
public Material newMat;
myMats = render.sharedMaterials
for(int i = 0; i < myMats.length; i++)
{
if(myMats*.someProperty == someCheck)*
{
myMats = new Material(newMat);
}
}
… or any variation of this, the Material simply doesn’t change.
Alternatively, I thought I could use something like this, changing the material’s properties, rather than the material altogether:
if(myMats*.someProperty == someCheck)*
{
myMats*.shader = newMat.shader;*
myMats*.CopyPropertiesFromMaterial(newMat);*
}
… This almost does exactly what I need, but if the material changes from an opaque rendering mode to a fade, for example, all of the properties change, but the material doesn’t render properly until I manually change something in the inspector (for example, nudging the color bar). After I touch the material manually, it corrects itself and renders as it should. This isn’t helpful though, as it should be automatically updated in game.
Is this a known Unity bug, or is there a way to update/refresh the shader after applying changes? Any feedback is appreciated. Here are some screenshots to better explain my problem. The first is the material before the change, the second is how it SHOULD look after the change, but the third is my actual result.
[49745-example.jpg|49745]