Change sprite dinamically

Hi, I’m having a problem trying to change a sprite. This is my code:

void Update()
if(changeSprite) {
spriteRenderer = GetComponent();

        GetComponent<SpriteRenderer>().sprite = newSprite;

        changeSprite = false;



With print(GetComponent().sprite) I’ve seen that the sprite’s name really corresponds to the correct sprite after change. Nevertheless, my character keeps with the same appearance. The script is correctly attached to the character (Other functionalities work). There’s an image attached to the question that show the inspector menu of my character.
Thank u in advance.

Hi, I could see an Animator Component attached to the character.

If I’m not mistaken the Animator would override any changes you make to the sprite since it’s also manipulating that same sprite variable of the sprite renderer.

Try disabling the Animator for a moment and see if it works.

Also, it’s usually best practice to cache any GetComponent calls since the Update method runs every frame. What I mean by this is:

private SpriteRenderer m_spriteRenderer;

void Start()
      // Call GetComponent in Start method
      m_spriteRenderer = GetComponent<SpriteRenderer>();

void Update()
      // now that it's cached call the spriteRenderer variable in place
      // of the GetComponent. 
      m_spriteRenderer.sprite = newSprite;