Player Hit Ignores Condition

Hello, so basically I want the player to hit a tree 3 times then it gets destroyed. Through my code the method in the collider works and it calls takeHit(); However in takeHit() the condition if (HitPoints <= 0) is not working and it destroys the tree right away after 1 hit (As it ignores the condition) . I have checked my if condition brackets and everything but I am still stuck with this problem.

Collision in Player class:

void OnCollisionStay2D(Collision2D collision)
{
    var tree = collision.collider.GetComponent<Tree>();

    if (Input.GetKey(KeyCode.S) && tree)
    {
        tree.takeHit(1);
        
    }

}

Tree Class

public int  MaxHitPoints = 3;
public int HitPoints;

 void Start()
{
    HitPoints = MaxHitPoints;
}

public void takeHit(int damage)
{

    HitPoints -= damage;

    if (HitPoints <= 0)
    {
        Destroy(gameObject);
    }

}

}

Your if statement is not the problem.

OnCollisionStay2D is your problem, as it is being called every frame the player is touching the tree’s collider.

You could use OnCollisionEnter2D or OnCollisionExit2D (depending on what you want to do).
Or if you are using a trigger collider, you could also use OnTriggerEnter2D or OnTriggerEnter2D.