unity change sprite of rendere code example
Example 1: unity ui change sprite
// TO change the sprite of an UI image, se below
GameObject.GetComponent<Image>().sprite = sprite;
Example 2: change sprite of a sprite unity
public SpriteRenderer spriteRenderer;
public Sprite newSprite;
void ChangeSprite()
{
spriteRenderer.sprite = newSprite;
}