I can change the color of a material with a URP lit shader just fine via code but when I try to to that to an URP unlit shader the color doesn’t change. Do I have to update the shader or material somehow?
I ran into the exact same problem - you savved me a lot of time by mentioning it works with lit shaders
Documentation says, that Material.color property is bound to shader property tagged as [MainColor].
Now if you look at source code of Lit and Unlit shaders you will see:
Lit:
[MainColor] _BaseColor("Color", Color) = (1,1,1,1)
Unlit:
_BaseColor("Color", Color) = (1, 1, 1, 1)
Unlit shader is missing [MainColor] and all other tags in its properties block.
So you cannot use Material.color with it.
What will work for both shaders is:
Material.SetColor( "_BaseColor", Color.red );```
Of course I strongly recommend using SetColor and GetColor with integer id of shader property rather than string name, but for this post it seemed clearer to use these versions of SetColor and GetColor :)
3 Likes
_emissionKeyWord = new(_roadMaterial.shader, “_EMISSION”);
_roadMaterial.SetColor(“_EmissionColor”, _roadConfig.SelectedRoadColor);
_roadMaterial.SetKeyword(_emissionKeyWord, false);
1 Like