Author Topic: [bug] UIButton cannot change color to grey while isEnabled = false in OnClick  (Read 13396 times)

yuewah

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 180
    • View Profile
UIButton cannot change color to grey while isEnabled = false is set inside OnClick

  1. public class ButtonOnClick : MonoBehaviour {
  2.  
  3.         void Start () {
  4.                 //this.GetComponent<UIButton>().isEnabled = false;
  5.         }
  6.        
  7.         void OnClick () {
  8.                 Debug.Log("OnClick");
  9.                 this.GetComponent<UIButton>().isEnabled = false;
  10.         }
  11. }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Changing the state of a button from inside a callback that results from the state changing...  iffy to begin with, but I'll have a look at it.

yuewah

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 180
    • View Profile
thank, actually, I just want to disable the Button when I click on it, the button is disabled, but the color didn't change to grey.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Assuming it's a UISprite on it.
  1. public class ButtonOnClick : MonoBehaviour {
  2.     void OnClick ()
  3. {
  4.         Debug.Log("OnClick");
  5.         GetComponent<UISprite>().color = Color.grey; //or (new Color(0.5f,0.5f,0.5f,1f)
  6.         gameObject.active = false
  7.  
  8.     }
  9. }
  10.  

Better (slightly faster) to just have the reference to whatever widget inside the ButtonOnClick though.

yuewah

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 180
    • View Profile
@Nicki, thank for your reply, it is a workaround. The problem is UIButton.isEnabled is not work as expected.