how to change image with script unity code example
Example 1: how to change a image with code unity
public GameObject GameObjectWithImage;
public Sprite ImageYouWant;
GameObjectWithImage.GetComponent<Image>().sprite = ImageYouWant
public Image Image;
public Sprite ImageYouWant;
Image.sprite = ImageYouWant
Example 2: set sprite unity
public Sprite sprite1;
public Sprite sprite2;
private SpriteRenderer spriteRenderer;
void Start ()
{
spriteRenderer = GetComponent<SpriteRenderer>();
if (spriteRenderer.sprite == null)
spriteRenderer.sprite = sprite1;
}
void Update ()
{
if (Input.GetKeyDown (KeyCode.Space))
{
ChangeTheDamnSprite ();
}
}
void ChangeTheDamnSprite ()
{
if (spriteRenderer.sprite == sprite1)
{
spriteRenderer.sprite = sprite2;
}
else
{
spriteRenderer.sprite = sprite1;
}
}