Change button color doesn't work

I’m trying to change the color of a Button in Canvas via scripting (image color and button color, both is not working right). It changes the color values, but switchs back to default colors a few lines later in my code:

public class OnAction : MonoBehaviour {

    public GameObject[] buttonScene = new GameObject[4];
    public GameObject [] subMenu = new GameObject[4];

...

    public void CallSubmenu(int subMenuID){

        if (subActive [subMenuID] == false) {
            subMenu [subMenuID].SetActive (true);
            Color btnColor = buttonScene [subMenuID].GetComponent<Image> ().color;
            ColorBlock btnColorH = buttonScene [subMenuID].GetComponent <Button> ().colors;
            btnColor = Color.grey;
            btnColorH.highlightedColor = Color.gray;
            btnColorH.normalColor = Color.gray;
            Debug.Log ("(b)color set: " + btnColor.b);
            for (int i = 0; i < buttonScene.Length; i++) {
                if (i == subMenuID)
                    continue;
                Color btnColorA = buttonScene [i].GetComponent <Image>().color;
//                Color txtColor = buttonScene [i].GetComponent <Text> ().color;
                btnColorA.a = colorAlpha;
//                txtColor.a = colorAlpha;
            }
            subActive [subMenuID] = true;

        } else {
            subMenu [subMenuID].SetActive (false);
            subActive [subMenuID] = false;
        }
        ColorBlock btnColorBl = buttonScene [subMenuID].GetComponent <Button> ().colors;
        Debug.Log ("BTNCOLOR: " + btnColorBl.highlightedColor);
        Debug.Log ("color set: " + buttonScene [subMenuID ].GetComponent <Image>().color.b);

    }

}

The OnAction.CallSubmenu is called via On Klick ()
The first log (line 17) shows the value 0.5 for the blue component which is set by Color.gray.
In the 2nd log (33/34) the values are already set back do default (1). Also changing the color via “highlightedColor” and “normalColor” does not fix the color values.
How can I fix the changed value of color components?

Thanks for help.

You’re part way there. You must assign the colour and/or colour block back to the image colour / button colours.
Right now, you’ve just altered copies but they “go nowhere/aren’t used”.

1 Like

Thank you @methos5k , helped a lot.
Got it now:)

can you please tell me how you will change the colour of a button by clicking on another button? or changing the colour of a list of buttons?

That’s what I do in the “for loop” (I just change the alpha component here, but you can apply this also to the b, g and r component of the color):

   public void CallSubmenu(int subMenuID){

        if (subActive [subMenuID] == false) {

...

            for (int i = 0; i < buttonScene.Length; i++) {
                if (i == subMenuID)
                    continue;
                Color btnColorA = defaultColor;
...
                btnColorA.a = colorAlpha;
                buttonScene [i].GetComponent <Image> ().color = btnColorA;
 ...
            }
            subActive [subMenuID] = true;

        } ...
...

    }

Here the loop changes the color of all other buttons, except the clicked button (subMenuID) (colorAlpha is a float between 0 and 1).

first can i use your code for my project?
second , can you please explain for me also?
here is my game design : i hope you help me . i cannot sleep because i cannot go more with that:
i have a ui button which is called : find it
i have then 5 buttons on game scene. i need each time that find it button is pressed … at each click , one of those 5 button change colours…or whatever …just it become obvouser to game user. please help me with and thanks.

@ :
Just write a function, e.g. “ChangeColor” which changes the color of the buttons in a script like I did with the “CallSubmenu”, put it on the On Click() on your “Find it” Button. So whenever you click the Find it Button, the ChangeColor function will be called.
The script calling via a button click is also explained in this tutorial: