Reputation: 1352
I just started to make 2D game for myself, I've got a problem. I have a gameObject moving through the sreen from right to the left, I need to destroy it when it leaves the screen. I've created "object_killer", added BoxCollider2D component, IsTrigger. This is my small script (C#):
void OnTriggerEnter (Collider other)
{
if (other.tag == "rektcar")
{
Destroy(gameObject);
}
}
But nothing happens - my gameObject is moving far away from the screen. "object_killer" has rektcar tag.
Upvotes: 0
Views: 167
Reputation: 145
Do both of the colliding objects have a 2D collider? Also, if you're making a 2D game with 2D colliders use
OnTriggerEnter2D(Collider2D) { }
Read this
Upvotes: 1
Reputation: 3925
Since you've used OnTriggerEnter
you should make sure your object has selected IsTrigger
property. If you don't want to use trigger you can change OnTriggerEnter
to OnCollisionEnter
.
Upvotes: 0