unity changesprite() code example
Example 1: 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;
}
}
Example 2: change sprite of a sprite unity
public SpriteRenderer spriteRenderer;
public Sprite newSprite;
void ChangeSprite()
{
spriteRenderer.sprite = newSprite;
}