- Home /
Emission colour only changes when RGB at 0
Hey,
So after a fair bit of googling, I've come up with a bit of a script to change the emission of a material (a little reactor thing on the side of the weapon) depending on the amount of ammo left in the weapon so that it can sort of serve as an indicator.
The only issue with it is that the emission colour only changes visibly when it reaches 0.
I've played around a little bit with it, but due to my inexperience with C# (I'm more or less a newbie to it) I'm not sure what the issue is.
This is my code with a fair bit cut out so it just removes ammo each update instead of when the weapon shoots (ammo is set to 51 by default by the way for ease of colour change):
public Material reactor;
public float ammo;
Vector4 emit = new Color(0f, 255f, 0f, 1f);
public void Update() {
Renderer renderer = GetComponent<Renderer> ();
reactor.SetColor ("_EmissionColor", emit);
if (ammo >= 1) {
ammo--;
float emitchange = emit.y - 5f;
emit = new Vector4(emit.x, emitchange, emit.z, emit.w);
reactor.SetColor("_EmissionColor", emit);
} else {
Debug.Log("Ammo Empty");
}
}
For some reason, it just refuses to show any change in the emission until emit.y reaches 0, meaning that I don't get the effect I want from it.
I hope someone knows what to do, thanks!
Answer by Namey5 · Jun 05, 2020 at 06:45 AM
Colours in Unity are represented as fractions, not integers. If you are trying to initialise the colour to green, you should do the following;
Vector4 emit = new Vector4 (0f, 1f, 0f, 1f);
...
emit.y -= (5f / 255f);
reactor.SetColor ("_EmissionColor", emit);
Your answer
Follow this Question
Related Questions
How change Emission color of material together with particles system"Color over lifetime" ? 0 Answers
Why does changing this object's color using Material.color work at all? (C#) 1 Answer
Particle system does not re-emit until Start Lifetime elapsed 4 Answers
Changing Emission Scale UI with Script 0 Answers
Emit particles by distance of parent 2 Answers