Enemies Destroyed When Health Still Greater Than Zero

My enemies are being destroyed after a single shot, despite some of them having their health increased to require more. I’ve searched around and gone through my code, but I can’t figure out why my condition isn’t functioning as expected.

I’ve logged to the console their health values, and they are taking the expected damage, and are being destroyed before their health values reach zero.

Here’s a screenshot of the applicable code:

Here’s a shot of the enemy’s prefab:

Here’s a shot of the player projectile prefab:

Screenshot 2021-03-20 111859

And lastly, here’s a shot of the console output:

Screenshot 2021-03-20 111550

Hi Charly,

Have you already tried to add Debug.Logs to your code to see what is going on during runtime? Maybe the collider gets hit twice in one frame.

And are you sure that the “hit” destroyed the enemy? A common reason for enemies getting destroyed is that they shoot themselves or that they are instantiated inside the shredder collider. Before you waste your time looking for the issue at the wrong place, try to figure out what might have triggered the Die method or, if the Die method was not called, what else might have destroyed the enemy.

The source of the issue wasn’t in my code at all - the EnemyHit() method was behaving properly, the object destruction was coming from the DamageDealer script. I had written mine to allow the player to destroy the projectiles, but I hadn’t matched the health on the Enemy settings between the DamageDealer and Enemy scripts. Once I matched the Health values between the two components, the issue resolved. (the DamagerDealer field was set to 100, which is the hit point value assigned to the player projectile, while the Enemy field was set to 300).

Screenshot 2021-03-21 151850

I’ve uncommented the OnTrigger method in the DamageDealer script, and the enemies and their projectiles now behave as expected.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms