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.