0
\$\begingroup\$

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);
        }
    }
}
\$\endgroup\$
3
\$\begingroup\$

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);
        }
    }
}
\$\endgroup\$
6
  • \$\begingroup\$ 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. \$\endgroup\$ – Tony Chacon Feb 24 '16 at 12:52
  • \$\begingroup\$ 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 \$\endgroup\$ – Hamza Hasan Feb 24 '16 at 12:56
  • \$\begingroup\$ 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. \$\endgroup\$ – Tony Chacon Feb 24 '16 at 13:15
  • \$\begingroup\$ 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 \$\endgroup\$ – Hamza Hasan Feb 24 '16 at 13:20
  • 1
    \$\begingroup\$ 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). \$\endgroup\$ – Tony Chacon Feb 25 '16 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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