- Home /
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:
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?
Answer by spanagiotis · Nov 03, 2014 at 01:17 AM
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 aBox Collider 2D
.Add a
Rigidbody2D
component to the object with theEdgeCollider 2D
.Check
isKinematic
on theRigidbody2D
component.Change your scripts method signature to use
void OnTriggerExit2D(Collider2D other)
.
Answer by N1warhead · Nov 03, 2014 at 01:02 AM
Not sure if u've tried
Destroy(this.gameObject);
That's what I've always used to destroy objects directly.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
destroying game object in a specific transform 2 Answers
Destroying an object after its out of sight ! 2 Answers
Turn Based RPG Combat System Style Game 0 Answers