Destroy function not working

Updated on December 6, 2018 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
1 on December 6, 2018

Hello i’m new to Unity and i’m having trouble with some coding. I’m developing a tower defense idle game. My problem is i need to destroy all my enemies when my tower hp reaches 0. Then i will be spawned the last wave i was able to beat. But only one or 2 enemies are being destroyed and some remain on the screen and either get destroyed by the tower adding gold or hit it reducing the hp before the new wave spawn.

Here is the code i’m using :

void Update() {

  1. if (TowerHp.Hp <= 0)
  2. {
  3. if (!run)
  4. {
  5. run = true;
  6. TowerHp.Hp = TowerHp.startHp;
  7. waveSpawner.EnemiesAlive = 0;
  8. waveSpawner.waveIndex-=2;
  9. Destroy(gameObject);
  10. return;
  11. }
  12. else
  13. {
  14. run = false;
  15. }
  16. }
  • Liked by
0 on December 6, 2018

What is “TowerHP”? Is that the HP of your main base you’re defending, or the HP of the tower the enemy is currently attacking?

Also, is this on every enemy? because this will drop your wave index by 2 for every enemy still alive at the time.

And what is “run” supposed to do? Because at best, all it does is delay this by 1 frame.


  • Liked by