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:
71140-capture1.png

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:
71141-capture2.png

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.

1 Answer

1

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.

I was having the same error and fix it by calling the Coroutines OUTSIDE the Update function, as stated here: // fix the warning IEnumerator LoadNextAsyncScene() One should also read this: [https://docs.unity3d.com/ScriptReference/AsyncOperation.html][1] [1]: https://docs.unity3d.com/ScriptReference/AsyncOperation.html