localScale is not working at the runtime after applying the Animator component

I apply an animator component to my Game Object. After that when I play the game, I try to change the localScale of the game object but it doesn’t work.

Here is my code to change the game object’s localScale according to its direction on the x coordinate.

private Rigidbody2D rb;
private Vector3 direction;
private Animator anim;
private bool isLookingRight;

void Start()
{
    rb = GetComponent<Rigidbody2D>();
    anim = GetComponent<Animator>();
    isLookingRight = false;
}

private void FixedUpdate()
{
    float horizontal = Input.GetAxis("Horizontal");
    TurnDirection(horizontal);
}

// turns the player towards to movement direction
private void TurnDirection(float horizontal)
{
    if (horizontal > 0 && !isLookingRight || horizontal < 0 && isLookingRight)
    {
        isLookingRight = !isLookingRight;
        direction = transform.localScale;
        direction.x *= -1;
        transform.localScale = direction;
    }
}

It worked well before applying the animation to the Game Object. I can’t change the scale of the Game Object even manually on the inspector while playing the game.

The animator will over-write what your code changes, if ant any point in the animation it animates the same thing that you are trying to change via code.
For example… if the animation changes position… you cant change it via code…
You can sort of get around this by putting your code in LateUpdate(). instead of Update()
but in general, you probably don’t want to have animations fighting with code… you’ll get unpredictable results.
If you remove the keys from the animation that access the transform / position… then your code will work as-is.

Another problem you might be having, is a rigidbody… if you have physics running on your object… then the physics will control the position and orientation of your object… so again, you’d have to be careful how your code interacts with physics.

Try not to have animations, physics and code fight for control over the same variables,
If you are using physics , then you control your object by applying forces and torques into the rigidbody, not by changing the position or rotation of the transform.

Suggest you take a look at the animation, physics and code… and make sure they are not modifying the same things.