Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved ColorBlock not properly applied to Button

Discussion in 'Editor & General Support' started by Okpower, Aug 11, 2023.

  1. Okpower

    Okpower

    Joined:
    Apr 25, 2020
    Posts:
    5
    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.
    Code (CSharp):
    1.     public ColorBlock colors;
    2.     private void ResetColors() //To reset the ColorBlock after using
    3.     {
    4.         colors = new()
    5.         {
    6.             normalColor = new(166, 12, 58), //Eg if I replaced this new(...) with Color.grey it would work but not otherwise
    7.             highlightedColor = new(166, 87, 111),
    8.             pressedColor = new(157, 119, 130),
    9.             selectedColor = new(157, 119, 130),
    10.             disabledColor = new(166, 12, 14),
    11.             colorMultiplier = 1,
    12.             fadeDuration = 0.1f,
    13.         };
    14.     }
    15.     public void ColorChange(Button button, int[] col, Color color) //Used to change the ColorBlock of a button
    16.     {
    17.         foreach (int i in col)
    18.         {
    19.             switch (i)
    20.             {
    21.                 case 1:
    22.                     colors.normalColor = color;
    23.                     break;
    24.                 case 2:
    25.                     colors.highlightedColor = color;
    26.                     break;
    27.                 case 3:
    28.                     colors.pressedColor = color;
    29.                     break;
    30.                 case 4:
    31.                     colors.selectedColor = color;
    32.                     break;
    33.                 case 5:
    34.                     colors.disabledColor = color;
    35.                     break;
    36.             }
    37.         }
    38.         button.colors = colors;
    39.         ResetColors();
    40.     }
     
  2. Okpower

    Okpower

    Joined:
    Apr 25, 2020
    Posts:
    5
    Screenshot of colors properties.
     

    Attached Files:

  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Okpower likes this.
  4. Okpower

    Okpower

    Joined:
    Apr 25, 2020
    Posts:
    5
    Switched it to new Color32 (...) and it worked perfectly. Thanks!
     
    Kurt-Dekker likes this.