Until I’m 100% sure that we can’t go over the maxHits variable, I figured it would be better to use >= when comparing them. Here is my solution.
private void OnCollisionEnter2D(Collision2D collision)
{
if (tag == "Breakable")
{
timesHit++;
if (timesHit >= maxHits)
{
DestroyBlock();
}
}
}
Edit: I’ve refactored the code to match what’s in the lecture and extracted the HandleHit() method.