2D: Destroy object with dynamic collider after exiting object with static collider
I have checked out countless similar threads all over the net and can't for the life of me figure out what I am doing wrong.
I have a static Box Collider 2D which covers the entire game area.
It has "Is Trigger" checked and contains a very simple script which should destroy every object that exits it.
using UnityEngine;
using System.Collections;
public class DestroyByBoundary : MonoBehaviour {
void OnTriggerExit(Collider other) {
Debug.Log ("OnTriggerExit");
Destroy (other.gameObject);
}
}
The player character shoots bullets which have a dynamic Circle Collider 2D.
However, the bullets are never destroyed when they leave the game area and just fly on indefinitely. The debug statement never gets executed either, of course. I have tried the same thing with OnTriggerEnter, which yields the same result.
If any more information should be needed, I'll be happy to provide it.
Answer by unityGecko · Sep 10, 2016 at 05:10 PM
The problem was that I was using 3D classes and methods in a 2D environment.
I had to change the parameter type of OnTriggerExit
to Collider2D
and the method itself to OnTriggerExit2D
.
using UnityEngine;
using System.Collections;
public class DestroyByBoundary : MonoBehaviour {
void OnTriggerExit(Collider other) {
Destroy (other.gameObject);
}
}
Your answer
Follow this Question
Related Questions
OnTriggerEnter2D() does not work !!! 0 Answers
Inconsistent 2d collider hit registration 3 Answers
Script to enable Firing 1 Answer
Can't seem to get collision to work among my sprites 1 Answer
OnTriggerEnter2D not called after collision (2D game) 2 Answers