unity change sprite image code example

Example 1: change sprite of gameobject unity

gameObjectName.GetComponent<SpriteRenderer>().sprite = spriteName;

Example 2: unity ui change sprite

// TO change the sprite of an UI image, se below
GameObject.GetComponent<Image>().sprite = sprite;

Example 3: change sprite of a sprite unity

public SpriteRenderer spriteRenderer;
public Sprite newSprite;

void ChangeSprite()
    {
        spriteRenderer.sprite = newSprite; 
    }

Example 4: unity set sprite image from script

Sprite myFruit = Resources.Load("fruits_1", typeof(Sprite)) as Sprite;

Tags:

Misc Example