- Home /
The question is answered, right answer was accepted
Changing the color at runtime is causing my material to look very different.
So currently I have a game object that is making use of a material with a "Legacy Shaders/Diffuse" shader and the main color is (255, 0, 0, 255) which is perfect and looks like this:
but I would like to change the color as the player progresses to the next level which I have done, the problem is when I apply the new material the game object looks completely different, it looks like this:
All my lights' Baking attribute are set to Realtime.
This is how I change the color:
Material material = new Material(Shader.Find("Legacy Shaders/Diffuse"));
material.color = new Color(255, 0, 0, 255);
Renderer rend = GetComponent<Renderer>();
rend.material = material;
Any help would be greatly appreciated.
Answer by allenallenallen · Jun 01, 2016 at 06:44 AM
Your 4 lines of code should just be 1 line if all you are changing is the color. You shouldn't need to assign a new material.
GetComponent<Renderer>().material.color = new Color(1, 0, 0, 1);
See if that changes anything.
Ok I replaced all my lines with yours and it still does the exact same thing. I started playing around with different shaders and I found that the "Unlit/Color" shader is basically what I am ending up with when I change the color with either my or your code. It kind of looks like the material is losing some attributes when changing the color, well that is all I can think of at the moment.
Huh, that's weird. $$anonymous$$aybe you need to reimport the shaders? The original shader of that object is Legacy Shaders/Diffuse, right?
Oh and, this might fix the color: https://docs.unity3d.com/ScriptReference/Color.html
It's new Color(1, 0, 0, 1) and not 255 because this is the new Color API.
Perfect. Thank you very much. The problem was the old api that I thought I was using. $$anonymous$$aking use of Color(1,0,0,1) is what I needed.