- Home /
How can I change the Value of a Color through script?
Im trying to make a brightness slider for a game that mixes 2d sprites and 3d objects. Since lighting doesnt really affect the sprites, i want to be able to change their brightness through code. I noticed that changing the alpha value does absolutely nothing unless its set to 0 (unless you do it in the editor, then it seems to work). I also saw they made a RGBToHSV method, but its a getter not a setter. Is there any way to change a sprites Value/Brightness through code?
Answer by Salocin19 · Mar 08, 2017 at 07:06 PM
In code, color values are "normalized." This means that values are <= 1 and >= 0.
In the unity editor, color values range from 0-255, for r,g,b and alpha.
If you wanted to set alpha to 127.5 (half of the max value) in code, then you would have to set alpha to 0.5.
Similarly, an alpha of 255 in editor = 1 in code and, an alpha of 0 in editor = 0 in code.
My guess is that, for example, if you wanted a sprite at half alpha, you attempted to set the alpha in code to 127.5. Since the values in the code are "normalized", anything above 1 will just have the alpha look like it's at its max value.
Hope this helps!
Your answer
![](https://koobas.hobune.stream/wayback/20220612093028im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Define pixels 1 Answer
Problem with a color randomization 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Instantiating multiple sprites and assigning different colors for each 1 Answer