How to destroy an object?

I have platforms in my game that I want to destroy as they pass through a boundary object. I can’t seem to get it working.

Here is the boundary object in the panel view:

34684-capture3.png

Here is the code attached to my boundary object:

public class DestroyByBoundary : MonoBehaviour {

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

And here is the platform object I am trying to destroy:

What am I doing wrong?

Your problem is mixing collider types. 2D and 3D collider functions don’t mix.

 void OnTriggerExit(Collider other)

Is for 3D colliders only (Box, Sphere, etc).

void OnTriggerExit2D(Collider2D other)

Is for 2D colliders only (Edge2D, Box2D, Circle, etc)

You will also need to put a Rigidbody2D on the object you want to detect via the collision. In this case you want to put it on the GameObject that has the EdgeCollider2D component attached it.

To make this work (I’m assuming a 2D environment, meaning restricted to x/y) do the following.

  • Change the BoxCollider to a Box Collider 2D.

  • Add a Rigidbody2D component to the object with the EdgeCollider 2D.

  • Check isKinematic on the Rigidbody2D component.

  • Change your scripts method signature to use void OnTriggerExit2D(Collider2D other).

Not sure if u’ve tried

Destroy(this.gameObject);

That’s what I’ve always used to destroy objects directly.