'Unity : I can't change the opacity of an image or change its color in RGB

in my project I want to change the color of a square at runtime which is simply a Game Object with an Image Component. In my script I proceed like this:

private Image imageRenderer;

...

imageRenderer = ImageCompleteSquat.GetComponent<Image>();
imageRenderer.color = Color.red;

In this case, it works and the image turns red as expected. But as soon as I change this line by putting my own RGB color like this : imageRenderer.color = new Color(227, 66, 52);

The image is not displayed anymore: it disappears. Does anyone know how to change the color of an Image Component?



Solution 1:[1]

you might want to set the alpha value first

imageRenderer = ImageCompleteSquat.GetComponent<Image>();
var tempColor = Color.red;
tempColor.a = 1f; // change this value (floating point between 0 and 1)
imageRenderer.color = tempColor;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dean Van Greunen