# Destroy 2 objects

**URL:** <https://phaser.discourse.group/t/destroy-2-objects/3100>\
**Category:** Phaser 3\
**Created:** [July 13, 2019, 3:59pm UTC](https://phaser.discourse.group/t/destroy-2-objects/3100 "2019-07-13T15:59:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Faizan\_Ali](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/faizan_ali/32/964_2.png) [@Faizan\_Ali](https://phaser.discourse.group/u/Faizan_Ali)\
**Post date:** [July 13, 2019, 3:59pm UTC](https://phaser.discourse.group/t/destroy-2-objects/3100/1 "2019-07-13T15:59:14Z")

</div>

Hi there, I have a class that holds my enemy tank and turret. Whenever my player tank destroys the enemy tank only the tank body seems to get destroyed and the turret stays. How do I make the turret disappear aswell?

Help would be much appreciated

class Enemy extends Phaser.Physics.Arcade.Sprite {  
constructor(scene, x, y, key, texture) {  
super(scene);  
this.scene = scene;  
this.x = x;  
this.y = y;

```
    this.enemy = this.scene.physics.add.sprite(x, y, 'enemy', 'tank1');
    this.enemy.setOrigin(0.5, 0.5);
    this.enemy.body.immovable = false;
    this.enemy.body.collideWorldBounds = true;
    this.enemy.body.bounce.setTo(1, 1);
    this.enemy.setVelocity(100, 0);
    this.enemy.health = 3;

    this.turret = this.scene.physics.add.sprite(x, y, 'enemy', 'turret');
    this.turret.setOrigin(0.3, 0.5);
    
    this.scene.add.existing(this);
    this.scene.physics.add.existing(this);
    this.scene.events.on('update', this.update, this);
}
update() {
    this.turret.x = this.enemy.x;
    this.turret.y = this.enemy.y;

    function enemyHitCallback(enemyHit, bulletHit)
    {
        // Reduce health of enemy
        if (bulletHit.active === true && enemyHit.active === true)
        {
            enemyHit.health = enemyHit.health - 1;
            console.log("Enemy hp: ", enemyHit.health);
    
            // Kill enemy if health <= 0
            if (enemyHit.health <= 0)
            {
               enemyHit.setActive(false).setVisible(false);
               
            }
    
            // Destroy bullet
            bulletHit.setActive(false).setVisible(false);
        }
    }

    this.scene.physics.world.collide(this.enemy, playerBullets, enemyHitCallback);
}

```

}

---

<div class="post-metadata">

**Author:** ![digitsensitive](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/digitsensitive/32/349_2.png) [@digitsensitive](https://phaser.discourse.group/u/digitsensitive)\
**Post date:** [July 13, 2019, 8:20pm UTC](https://phaser.discourse.group/t/destroy-2-objects/3100/2 "2019-07-13T20:20:09Z")

</div>

You have to call the destroy() function as follows:

```
this.turret.destroy()
```

---

<div class="post-metadata">

**Author:** ![Faizan\_Ali](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/faizan_ali/32/964_2.png) [@Faizan\_Ali](https://phaser.discourse.group/u/Faizan_Ali)\
**Post date:** [July 13, 2019, 9:28pm UTC](https://phaser.discourse.group/t/destroy-2-objects/3100/3 "2019-07-13T21:28:16Z")

</div>

I get this error:

Uncaught TypeError: Cannot read property ‘destroy’ of undefined

I put it here:

if (enemyHit.health \<= 0)  
{  
enemyHit.setActive(false).setVisible(false);  
this.turret.destroy();

```
            }
```

---

<div class="post-metadata">

**Author:** ![jjcapellan](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/jjcapellan/32/542_2.png) [@jjcapellan](https://phaser.discourse.group/u/jjcapellan)\
**Post date:** [July 14, 2019, 5:21pm UTC](https://phaser.discourse.group/t/destroy-2-objects/3100/4 "2019-07-14T17:21:03Z")

</div>

Hi @Faizan_Ali,  
It seems that the context is wrong.  
Try to add the context in the collide function:

```javascript
this.scene.physics.world.collide(this.enemy, playerBullets, enemyHitCallback, null, this);

```

Regards.

---

<div class="post-metadata">

**Author:** ![Faizan\_Ali](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/faizan_ali/32/964_2.png) [@Faizan\_Ali](https://phaser.discourse.group/u/Faizan_Ali)\
**Post date:** [July 14, 2019, 5:39pm UTC](https://phaser.discourse.group/t/destroy-2-objects/3100/5 "2019-07-14T17:39:01Z")

</div>

Thank you so so much, been trying for days to fix this problem! I really appreciate it

@jjcapellan
