4.6 New UI How to change Button Image?

Mastro picture Mastro · Jan 4, 2015 · Viewed 23.6k times · Source

Everything I search is based on the old UI.

I've tried the following

button.image = Resource.Load<Image>("...");
button.GetComponent<Image>() = Resources.Load<Image>("....");
button.GetComponent<Button>().image = Resources.Load<Image>("....");
button.GetComponent<Image>().sprite = Resources.Load<Sprite>("....");

I want to change the button image on an event.

Answer

Aizen picture Aizen · Mar 29, 2015

Tested and Working.

public Sprite myImage;
public Button myBtn;
void Start(){

         myImage = Resources.Load<Sprite>("BTNS"); // Make sure not to include the file extension

         //Make sure it is added in the Inspector. Or reference it using GameObject.Find.
         myBtn.image.sprite = myImage; // That is right, no need to GetComponent.

}