unity set sprite code example
Example 1: change sprite of gameobject unity
gameObjectName.GetComponent<SpriteRenderer>().sprite = spriteName;
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;
}
}
Example 3: change sprite of a sprite unity
public SpriteRenderer spriteRenderer;
public Sprite newSprite;
void ChangeSprite()
{
spriteRenderer.sprite = newSprite;
}
Example 4: sprite unity script
Resources.Load <Sprite>