button delete

I wondered how to delete a “Source Image” of a button component in code?

Thanks to you,


The answer was posted here:

 using UnityEngine;
 using UnityEngine.UI;
 public class example : MonoBehaviour
     public Sprite newsprite;
     public bool condition;
     private Button button;
     void Start ()
         button = GetComponent<Button>();
     void Update ()
         if (condition)
             // If you want to change the sprite for only a short time,
             // and use a default whenever your condition is false
             button.image.overrideSprite = newsprite;
             // But if you really want the source image,
             // use the following line instead
             // button.image.sprite = newsprite;
             // Setting the overrideSprite back to null will cause
             // the image to display the original value of image.sprite again
             button.image.overrideSprite = null;

Do you mean the “Image” component of a button? Because you can simply say Destroy(GetComponent<Image>()) (just make sure that at the beginning of your script you have using UnityEngine.UI;

However depending on your situation, it might be a better idea to just deactivate the image instead ( GetComponent<Image>().enabled = false);