The Wayback Machine - https://web.archive.org/web/20161019024426/http://gamedev.stackexchange.com/questions/117187/unity-5-3-c-how-to-disable-multiple-game-objects-with-one-collision
Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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); } } } 
share|improve this question

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); } } } 
share|improve this answer
    
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. – KorudoFeb 24 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 HasanFeb 24 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. – KorudoFeb 24 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 HasanFeb 24 at 13:20
1  
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). – KorudoFeb 25 at 12:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.

close