Perhaps you have “Write Defaults” checked in the animation state?
NOTE: If you animate what Sprite is shown that will overwrite stuff you do in code, unless you put your overwrite code in LateUpdate(). Here’s why:
Meanwhile, here’s just general animation stuff to note:
Anything with Animations / Animators / Mechanim:
Only consider the code AFTER you have done this critical step:
Always start with the Animator state machine and prove it works in isolation, no code at all.
Here’s more reading: