how to change the sprite in sprite renderer through script in unity code example
Example 1: change sprite of gameobject unity
gameObjectName.GetComponent<SpriteRenderer>().sprite = spriteName;
Example 2: change sprite of a sprite unity
public SpriteRenderer spriteRenderer;
public Sprite newSprite;
void ChangeSprite()
{
spriteRenderer.sprite = newSprite;
}