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.