C# Invalid Normalized Color
I keep getting this error after attempting to assign my 2D Sprite's Color. What exactly does it mean? Is it some sort of issue with the values? They do range from 0-255 which to my knowledge are the highest/lowest values color can go.
void Awake(){
GetComponent<SpriteRenderer>().color = new Color(Random.Range(0, 255),Random.Range(0, 255),Random.Range(0, 255), 255);
}
Answer by smnerat · Aug 01, 2016 at 03:47 AM
Generally, yes, color values range from 0 to 255. This is because 255 is the largest number possible using 8 bits, that way if your texture format is something like RGB24 or RGB32, you can use 3 or 4 color channels. Everything works out great.
This is far from an absolute, in Unity and many other applications, the Color structure is made of 4 floating point values ranging from 0 to 1. I would guess this makes passing around color values much easier for the engine.
So in your case, you just need to divide each of your values by 255 so they are in the range from 0 to 1.
FYI, graphics cards use 0-1 for color, so any serious game engine is also going to do that. It's not a Unity thing.
Yoo thanks so much, I was searching for all over why my color wasn't turning out right- just what I needed to know!
Thanks, this solved my problem too. Just be careful, if you just divide your Randoms with 255, its gonna be an integer division so basically everything will turn black. You have to cast it to float, so that it can use the (float, float, float) constructor.