OnTriggerEnter question.

Hi! I’m new with unity and i’m doing the space shooter tutorial, but i want to add an special shoot (A missil), which basically, when it hits something, it enable a sphere collider arround the explosion, and i would want that every object inside the explosion, to dissapear… I’ve made this, it shoots the missil, it instantiate the explosion, but objects inside the explosion area are not destroyed:

void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Missil")
        {
            missilCollider.enabled = true;
            Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
            gameController.AddScore(scoreValue);
            Destroy(gameObject);
            Destroy(other.gameObject);
        }

	
	}

The “missilCollider” is the sphereCollider attached to the asteroid, and its desabled by default.
I would appreciate any help, and sorry for the bad english ^^

Here’s the full code if it’s necessary:

using UnityEngine;
using System.Collections;

public class DestroyByContact : MonoBehaviour {

    public GameObject explosion;
    public GameObject playerExplosion;
    public int scoreValue;
    private GameController gameController;
    public SphereCollider missilCollider;

    void Start()
    {
        GameObject gameControllerObject = GameObject.FindWithTag("GameController");
        if (gameControllerObject != null)
        {
            gameController = gameControllerObject.GetComponent<GameController>();
        }
        if (gameControllerObject == null)
        {
            Debug.Log("No se encuentra 'GameController' script");
        }
    }

    void OnTriggerEnter(Collider other)
    {
        
        if (other.tag == "Boundary")
            return;
        Instantiate(explosion, transform.position, transform.rotation);
        if (other.tag == "Player")
        {
            Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
        }
        if (other.tag == "Missil")
        {
            missilCollider.enabled = true;
            Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
            gameController.AddScore(scoreValue);
            Destroy(gameObject);
            Destroy(other.gameObject);
        }
        gameController.AddScore(scoreValue);
        Destroy(other.gameObject);
        Destroy(gameObject);
	
	}


}

You need at least one rigidbody. For instance missile can have rigidbody and a spherecollider.

Your asteroids must have spherecollider to and Check isTrigger checkbox.

I think you could do this two ways, but I’ll only go over one. The other way I’m thinking is extremely similar, but instead of attaching the sphere to the missile, you have to have them attached to the individual asteroids. If the explosion will always be the same size, regardless of the size of the asteroid hit, you could attach a sphere (as a child) that represents the size of the explosion to the missile, then remove the mesh renderer from the sphere. On the missile, have something like this code:

using UnityEngine;
using System.Collections;

public class MissileScript : MonoBehaviour {

    public bool isHit = false;
    private GameObject expRadius;

	// Use this for initialization
	void Start () {

        expRadius = GameObject.Find("ExplosionRadius");
        expRadius.SetActive(false);

	}
	
	// Update is called once per frame
	void Update () {
	
        if (isHit)
        {
            expRadius.SetActive(true);
            isHit = false;
        }
        else
        {
            expRadius.SetActive(false);
        }

    }
}

Then, when the missile hits something, set isHit to true. This will enable the sphere gameobject, which contains the trigger to destroy everything inside. Attach the following script to the sphere:

using UnityEngine;
using System.Collections;

public class ExplosionRadiusScript : MonoBehaviour {


    void OnTriggerEnter(Collider other)
    {
        Destroy(other.gameObject);
    }

}

This will cause the sphere gameobject to be enabled when the isHit bool becomes true, destroys everything inside it, then disables it again. My example I set up looks like this: