- Home /
button delete
I wondered how to delete a "Source Image" of a button component in code?
Thanks to you,
A+
Answer by wrober · Aug 24, 2017 at 02:09 PM
The answer was posted here:
http://answers.unity3d.com/questions/807557/how-to-access-sourceimage-component-of-a-button-in.html
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Button))]
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;
}
else
{
// Setting the overrideSprite back to null will cause
// the image to display the original value of image.sprite again
button.image.overrideSprite = null;
}
}
}
Answer by SneakyLeprechaun · Aug 24, 2017 at 02:12 PM
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
);
Your answer
Follow this Question
Related Questions
How do I make buttons that turn into a text field? 2 Answers
assign keyboard buttons 1 Answer
Problem making buttons from array of array 1 Answer
Random Button / Plane Generation. 0 Answers