UNITY – JS Script – OnTrigger – Destroy

It will destroy the object if it enters or exit the trigger

1. Create a Sphere that falls and bource over a Box (You have to use Physics 3D engine)

2. Select the Box> Inspector> Box Collider> check ‘Is Trigger’, now the Box does not create collision, instead the Ball pass through it and this can be detected via code.

3. Attach to the Box the script

It will destroy the object if it enters the trigger


#pragma strict

function Start () {

}

// Destroy everything that enters the trigger START

	function OnTriggerEnter (other : Collider) {
		Destroy(other.gameObject);
	}

// Destroy everything that enters the trigger END


function Update () {


}

OR

It will destroy the object if it exit the trigger


#pragma strict

function Start () {

}

// Destroy everything that leaves the trigger START

	function OnTriggerExit (other : Collider) {
		Destroy(other.gameObject);
	}

// Destroy everything that leaves the trigger END


function Update () {


}