# Unity 5.3 C#: How to disable multiple game objects with one collision?

I'm making a top down perspective game with a gunner automatically moves on a pre-set path, and you shoot and destroy targets to remove barriers. The idea is that you control the bullet's direction after firing and it can pass through laser gates, but the gunner cannot. There are several targets and barriers.

What I want: If the bullet hits the local target, the local target and local barrier are destroyed.

I tried making each target and barrier as children of empty game objects, and I attempted to destroy the empty parent to solve this problem. Please advise.

(Showing only relevant code)

using UnityEngine;
using System.Collections;

public class MagicBulletScript : MonoBehaviour {

public Transform magicBulletTransform;

void Start () {
magicBulletTransform = transform;
}

void OnTriggerEnter (Collider other){
var progressParent = this.transform.parent.gameObject;

if (other.gameObject.CompareTag("Obstacle")){
Destroy (this.gameObject);
}

if (other.gameObject.CompareTag("Target")){
Destroy (progressParent.gameObject);
Destroy (other.gameObject);
}
}
}


Order does matter man..

First destroy obstacle then bullet itself, otherwise after destroying itself it will never execute next lines. And once you destroy own parent then no need to destroy child (itself).

And there is no need to keep own transform in a public Transform, you can access it anywhere through transform.

using UnityEngine;
using System.Collections;

public class MagicBulletScript : MonoBehaviour {

//public Transform magicBulletTransform;

void Start () {
//magicBulletTransform = transform;
}

void OnTriggerEnter (Collider other){
var progressParent = transform.parent.gameObject;

if (other.gameObject.CompareTag("Target")){
Destroy (other.gameObject);
}

if (other.gameObject.CompareTag("Obstacle")){
Destroy (progressParent);
}
}
}

• I apparently didn't convey what I meant. The only reason I showed the Obstacle was to show that the bullet is meant to be destroyed by obstacles, not the target. In other words: the bullet will pass through the target upon collision, but will be destroyed if it hits the terrain (obstacle-tagged objects). Barrier and Obstacle are completely different game objects. Sorry for confusion. My question is: "Can I destroy multiple game objects that have different tags with a single collision simultaneously?" The idea is to destroy the target, which destroys the barrier, so the gunner can proceed. – Tony Chacon Feb 24 '16 at 12:52
• As you said, you parented target and barrier and destroying the parent. So it should work, although I can't see it in your code – Hamza Hasan Feb 24 '16 at 12:56
• I forgot to give the parent game object a RigidBody. That seemed to work. Thanks. Now I'm trying to figure out how to destroy a single instance of an other.gameobject-tagged object, opposed to destroying all of them at once. – Tony Chacon Feb 24 '16 at 13:15
• If this helped you out then mark it as answer (if you feel no harm in it) and ask a separate question with scenario and code so other members also can understand and can help you out – Hamza Hasan Feb 24 '16 at 13:20
• Adding a rigidbody without marking "Is Kinematic" would have the gameObject fall through the scene. Everything appeared to be destroyed at once, but really just fell through the level. Everything else works (for reference if anyone else reads this). – Tony Chacon Feb 25 '16 at 12:35