- Home /
Change Color in C# with RGB Values?
Hi everyone, I've been writing a script in C# to change the color of an object when pressing down a key on the keyboard, and that works fine if the code is written like this:
void Update ()
{
if(Input.GetKeyDown(KeyCode.R))
{
gameObject.GetComponent<Renderer>().material.color = Color.red;
}
}
However, instead of changing the color to Red, I want to change it to a color with certain RGB values. (233, 79, 55) Is there a way to do this? I've tried everywhere, but I can't seem to find anything.
Thanks.
EDIT: Ok. So I think I'm getting somewhere. I've tried creating a script:
void Update ()
{
if(Input.GetKeyDown(KeyCode.R))
{
gameObject.GetComponent<Renderer>().material.color = new Color(233, 79, 55);
}
}
I think this might work, however when I test the game it changes the material color to white. Does anyone know what I can do to fix it?
Answer by Cherno · Jul 06, 2016 at 09:59 PM
The problem with your updated script: You pass values above 1.0 to a variable of type Color, which takes values from 0.0 to 1.0, so it ignores anything higher than that and treats it as 1.0. That's why your color is white in the game. The solution is simple: Use Color32 instead, which takes a value from 0 to 255.
Or you can divide by 255.
new Color(233f/255f, 79f/255f, 55f/255f);
Not saying it's better, just felt that showing how to fix the OP's code with value changes helps to emphasize what they've done wrong.
Answer by TheBestUnity123 · Oct 19, 2018 at 09:02 AM
@Matthew.OC But in Color32 ,there is a 4th argument which is a or transparency.
In a Color, you can use:
// C# example
Color c = new Color(0.5f, 0.5f, 0.5f);
// Same as...
Color c = new Color(0.5f, 0.5f, 0.5f, 1.0f);
By correlation, the alpha channel would be at the maximum value of 255:
Color32 c = new Color32(127, 127, 127, 255);
Answer by bigChris · Jan 25, 2019 at 07:56 PM
Lets say you've come up with the perfect shade of grey (or gray). In the color picker, you note that "Perfect Grey" is "ADADAD" and the Alpha is 200. In your game, the myGameObject's color gets changed to Red for whatever reason, but later, your GameObject is newly invigorated and its color must change back to "Perfect grey". The call to do this is:
myGameObject.color = new Color32( 173, 173, 173, 200 );
Where did the 173 values come from? The "AD" in the color picker is a hex value. Convert that to a decimal and you have "173". If you didn't get a BS in Computer Science and/or can't convert that in your head, there are any number of Hex to Decimal converters on line. The Alpha (opacity) value "200" is conveniently displayed in the color picker as a decimal.
Answer by Rugbug_Redfern · Jan 27, 2019 at 06:20 PM
The Color class uses values from 0 to 1, not 0 to 255. So your color should be
new Color(0.91, 0.3, 0.21);
Your answer
Follow this Question
Related Questions
Material doesn't have a color property '_Color' 4 Answers
Changing two different objects renderer colour 1 Answer
Gui list and color 0 Answers
Why are there color differences between photoshop and Unity? 5 Answers
Simple question about materials 1 Answer