Destroying an object when player collides with another object?

hey, i’d like to know how I can destroy a specific object when the player collides with another invisible object, like if the player collides with an invisible object a box in front of him would disappear, thanks!

This is a fairly common question that is asked and could be found by googling… However i’m not going to be “that guy”.

SO, what you could do, is on the object that you want to have destroyed, add another BoxCollider (2D or 3D) and make it “IsTrigger”.

The “IsTrigger” setting makes that specific collider, still register collisions, but doesn’t actually touch anything. Make this “IsTrigger” collider as big as you need it by clicking “Edit Collider”. Sort of like having a box full of water and sticking your hand in, vs a box full of concrete. You still register the “collision” of water, but you go through instead of colliding, like the concrete. Get it? Cool!

NOW, add a script called “OnTriggerDelete”, or whatever you’d like to call it. Open it up, and add one of these functions

OnTriggerEnter(Collider coll){
    if(coll.tag == "Player"){ //"coll" is reference to the object that collided with the trigger
        Destroy(gameObject); //Destroy ourselves
    }
}

//OR IF YOU ARE DOING 2D

OnTriggerEnter2D(Collider2D coll){
    if(coll.tag == "Player"){ //"coll" is reference to the object that collided with the trigger
        Destroy(gameObject); //Destroy ourselves
    }
}

This should get you going :slight_smile:

NOTE: You still need your non “IsTrigger” collider on your object!

// Hope this will work for you
using UnityEngine;
using System.Collections;

public class EnemyDestroyer : MonoBehaviour {

void OnCollisionEnter2D(Collision2D col){

	if (col.gameObject.tag == "enemy") {
	
		Destroy(col.gameObject);

	}
}

}