- Home /
Why does changing this object's color using Material.color work at all? (C#)
There are a couple places in my project where I need something to change color. I'm not using any light sources; instead, I've been setting the emission color in the material settings. In my game, the player has a flamethrower, which overheats when you use it for too long (so you have to let it cool down). Recently, I wanted the color of my player to change from black to red, in order to indicate that the flamethrower was overheating. I first tried with this code, in Update():
bodyMaterial.color = Color.Lerp(blackColor, redColor, firingTimePassed / flamethrowerOverheatTime);
This didn't work, because using the Material.color variable was the 'same as using GetColor or SetColor with "_Color" name' (Unity Script Reference). Without any light sources, the color seems to stay the emission color (black). So, I updated this code to:
bodyMaterial.SetColor("_EmissionColor", Color.Lerp(blackColor, redColor, firingTimePassed / flamethrowerOverheatTime));
Which works perfectly. But... this led me to remember another piece of code I wrote much earlier, for the fire particles themselves. I wanted the fire particles to appear with slight variations in color, so in the Start() function I wrote:
material = gameObject.GetComponentInChildren<Renderer>().material;
float randomRednessModifier = Random.Range(-colourOffsetRange, colourOffsetRange);
Color color = material.color;
color.r += randomRednessModifier;
material.color = color;
And it works. But why? How? There aren't any light sources, so I would've thought I'd need to change the _EmissionColor property rather than the _Color property? Just so you know, the player is completely black by default, and the fire particles are completely red.
Answer by toddisarockstar · Jul 10, 2017 at 08:04 PM
the simplest way to change the main color of the material shader is like this:
transform.renderer.material.color=Color.red;
however that just changes the main color. if your shader has multiple color options you can change the other colors like this:
transform.renderer.material.SetColor("_SpecColor", Color.red);
if your shader has slider bars or adjustable floats you can change them like this:
transform.renderer.material.SetFloat("_Shininess",0.5f);
anyways part of your problem is that if your character's actual texture is black. it will always be black. When the shader's color propery tints a color. It just muliplys the two colors for the output. so colors can be deepend and darkened but not lightened. in other words you can't lighten a black texture to be red. the tint color does nothing to black. (since the RGB of black 0,0,0,0 * whatever tint color will always be zero).
you could make your original texture white. and use the color property to make it black. then you would be able to switch to red.
other than that you could change some reflective color (with my second code example above) and hope light reflection makes him look more redish.
Your answer
Follow this Question
Related Questions
My foreach or the Resources.LoadAll Is not getting the information I need 2 Answers
How to change material color to a specific object group's childs..? 1 Answer
Array of colors not working, all sprites turn white. 1 Answer
Changing Splash Screen background Color with script?HELLLLP 1 Answer
Apply gradient between two colors on x amount of objects 1 Answer