Animation playing before enemy is assumed dead (Destroyed) by script.

Hello I am new to C# and I have an issue with the enemy animation. I have a player who shoots a bullet to enemy and enemy dies after 3rd hit. The script below:

// Start is called before the first frame update
void Start()
{
    rb = this.GetComponent<Rigidbody2D>();
    anim = GetComponent<Animator>();
    playerposition = GameObject.FindWithTag("Player").transform;
}

// Update is called once per frame
void Update()
{
    if(!IsDead)
    {
        Vector3 directions = playerposition.position - transform.position;
        directions.Normalize();
        movement = directions;
    }
    
}
private void FixedUpdate()
{
    if (!IsDead)
    {
        moveCharacter(movement);
    }
}
void moveCharacter(Vector2 direction)
{
    if (!IsDead)
    {
        rb.MovePosition((Vector2)transform.position + (direction * moveSpeed * Time.deltaTime));
    }
}
public void OnCollisionEnter2D(Collision2D boom)
{
    if (boom.gameObject.name == "bullet(Clone)")
    {
        Destroy(boom.gameObject);
        life += 1;
        if (life == 3)
            gameObject.GetComponent<CircleCollider2D>().isTrigger = true;
            anim.SetBool("isDead", true);
            Destroy(gameObject, 40f);
    }
}
public bool IsDead
{
    get
    {
        return life >= 3;
    }
}

}

The line that defines the enemy death is OnCollisionEnter2D which adds/subtracts a hit point to enemy until it reaches 3. Animation is supposed to be triggered when if (life == 3). However animation is being triggered right after the first hit and enemy keeps shooting and moving. If I referenced bool “isDead” as true shouldn’t animation still only be true when the if() variable is met? If anyone has any idea why this is happening I would appreciate it. Thank you!

Hi eperez2208, in C#, if there is more than one line of execution code for the if statement, you need to place whole code in the brackets.
Example:

if (a == 3) DoSomething();

if(a == 3)
       Do Something(); 

but:

 if (a == 3) 
{ 
   DoSomething();
   DoSomething();
   DoSomething();
}

So, in this case you should use:

if (life == 3)
{
         gameObject.GetComponent<CircleCollider2D>().isTrigger = true;
         anim.SetBool("isDead", true);
         Destroy(gameObject, 40f);
 }