- Home /
How to change Image on button click in a prefab that is instantiated
I am wanting to change a UI image that is a part of a prefab that I am instantiating in a 3D world by clicking different buttons in the UI.
I don't want to make a bunch of prefabs where the only thing that is changing is the image. It would be ideal if it changed the image component of the prefab so when I wanted to swap the image it would only change the ones that are instantiated after the change.
Does this make sense?
All input would be very helpful! Thank you in advance.
Answer by LuisVazquez · Jul 11, 2018 at 11:28 PM
Hi, I don't know if this is exactly what you mean and need but hope it would help.
public Button button;
public Sprite newImage;
void Start()
{
button = GetComponent<Button>();
}
public void ChangeImage()
{
button.GetComponent<Image>().sprite = newImage;
}
Your answer
Follow this Question
Related Questions
Instanciate prefab responsively 1 Answer
Instantiated UI prefab in canvas does not render image 1 Answer
Instantiate rect transform objects 2 Answers
text and image prefab not popping up when set.active 2 Answers
How to instantiate a projectile only from the weapon prefab of the firing player? 1 Answer