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);
}
}
}