Change color of MaterialButton Runtime

0 votes
asked Apr 28, 2016 in Help by artenio (280 points)
Hello, i want to change the color of a button when it is pressed.

I'm using a C# script in the EventManager and i'm doing this:

 
//Declare the Button, then in the UnityEditor Drag&Drop the button inside the properties of the script.

private Button toggleReadyStatus = null;

//Assign a function to be called when the "onClick" event is fired.

void Start () {
        toggleReadyStatus.onClick.AddListener(() => { SendToggleReadyStatus(); });
}

//The function called by onClick

private void SendToggleReadyStatus()
    {
        imReady = !imReady;

        SetReadyStatus(imReady);

    }

 public void SetReadyStatus(bool status)
    {

        if (status)
        {
           GameObject.Find("ImageInsideButtonRenamed").gameObject < Image >().color = Color.green;
        }
        else
        {
            GameObject.Find("ImageInsideButtonRenamed").gameObject < Image >().color = Color.red;
        }
    }

1 Answer

0 votes
answered May 2, 2016 by admin (31,720 points)
selected May 4, 2016 by artenio
 
Best answer
Hi there,

If a MaterialButton object also contains a MaterialRipple component, then you can change the color of the button by calling SetGraphicColor() on the MaterialRipple component. If there isn't a MaterialRipple component, then directly setting the color of the Image (like you did) should work just fine.

Hope that helps :)

~ Declan.
commented May 4, 2016 by anonymous
edited May 4, 2016
ReadyBtnImage.GetComponent<MaterialRipple>().SetGraphicColor(Color.green);

I have used this, but i get this error:

NullReferenceException: Object reference not set to an instance of an object

 

 

commented May 4, 2016 by artenio (280 points)
Solved, My mistake, the code is working :)

Artenio.
commented May 4, 2016 by admin (31,720 points)
Good to know :)

~Yohan
Welcome to MaterialUI support! Ask us anything :)
...