ColorBlock not properly applied to Button

Attempting to change the color tint to my button in code. However when used the colors default to white in game. Looking at them in the properties of the button reveals them to have the correct RGB values and show the correct color when opened in the color editor but still show white otherwise. As soon as I change any variables in the color editor the correct color immediately updates. If I instead assign a preset Color.grey or red ext, the code will work properly but not if I attempt to create a color using RGB values.

    public ColorBlock colors;
    private void ResetColors() //To reset the ColorBlock after using
    {
        colors = new()
        {
            normalColor = new(166, 12, 58), //Eg if I replaced this new(...) with Color.grey it would work but not otherwise
            highlightedColor = new(166, 87, 111),
            pressedColor = new(157, 119, 130),
            selectedColor = new(157, 119, 130),
            disabledColor = new(166, 12, 14),
            colorMultiplier = 1,
            fadeDuration = 0.1f,
        };
    }
    public void ColorChange(Button button, int[] col, Color color) //Used to change the ColorBlock of a button
    {
        foreach (int i in col)
        {
            switch (i)
            {
                case 1:
                    colors.normalColor = color;
                    break;
                case 2:
                    colors.highlightedColor = color;
                    break;
                case 3:
                    colors.pressedColor = color;
                    break;
                case 4:
                    colors.selectedColor = color;
                    break;
                case 5:
                    colors.disabledColor = color;
                    break;
            }
        }
        button.colors = colors;
        ResetColors();
    }

Screenshot of colors properties.

Not all RGB values are created equal.

Color() accepts channel params from 0.0f to 1.0f

Color32() accepts channel params from 0 to 255

More on Color vs Color32, lossless, comparisons, etc.

https://discussions.unity.com/t/891296/2

1 Like

Switched it to new Color32 (…) and it worked perfectly. Thanks!

1 Like