- Home /
How to access SourceImage component of a button in new GUI 4.6
hello community,
in the new GUI a button has an Image script attached to it by default. Source Image is the field you put the image that the button will have. What i want is to be able to change that image in code c# at runtime after a condition is met.
Can you help me ?
thanks for your time
Answer by troien · Oct 13, 2014 at 09:45 AM
First of all, you need "using UnityEngine.UI" to be able to access Buton and/or Image components from your script. After that, all you need is a reference to the Image your button is using and then change the sprite or overrideSprite property's of that Image to your new Sprite
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;
}
}
}
Thanks a lot this did it :) This can be applied to one button though, what if i have more buttons and i need to change the image on a specific one ?
Im not certain if I understand this last question, because I believe this is possible by only adding this script to your specific button.
The only thing I can come up with is that you want to put this script on something else then a button, and assign your specific button to it through the inspector. To do this simply remove the "RequireComponent(typeof(Button))" attribute and the "Start" function. And make the "button" public to show it in the inspector...
I have several buttons and I need to change a specific one's image when a certain condition is met. If i apply this script to these buttons and this condition is met then all the buttons change with the new image not a specific one I want.
Well, then you should add this script to your specific button that you want to change when your condition is met. DON'T add this script to any button that shouldn't change when the condition is met...
void Cars(int carscore) {
// changes the sprites to allow rewards of 1 , 2, 3 cars according to score
images = gameObject.GetComponent<Image>(); // get the component of Image method
if (carscore == 1) {
images.sprite = OneCarSprite;
} else if(carscore == 2) {
images.sprite = TwoCarSprite;
} else if(carscore == 3) {
images.sprite = ThreeCarSprite;
} else images.sprite = NoCarSprite;
}
Answer by juharain · Nov 25, 2015 at 03:13 PM
You can do this without variables.
GameObject.Find("carscore1").GetComponent<Button>().image.overrideSprite = null;
Now you can change the sprite in those buttons seperately. Just change the game objects name.