2
\$\begingroup\$

I'm using the Unity5 game engine and am programming a 2D shooter in bird's eye view.

I have a static Box Collider 2D which covers the entire game area.

enter image description here

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.

enter image description here

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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Looks like you're mixing 2D & 3D physics. Try using OnTriggerExit2D and see if that solves the problem. \$\endgroup\$
    – DMGregory
    Sep 10, 2016 at 16:01

1 Answer 1

3
\$\begingroup\$

As DMGregory has pointed out in a comment, I was using 3D objects and methods in a 2D environment.

I had to change the parameter of the script to a Collider2D object and use the OnTriggerExit2D method.

 using UnityEngine;
 using System.Collections;

 public class DestroyByBoundary : MonoBehaviour {
     void OnTriggerExit2D(Collider2D other) {
         Destroy (other.gameObject);
     }
 }
\$\endgroup\$
1
  • \$\begingroup\$ Please mark this as the accepted answer, for future readers. \$\endgroup\$
    – nihohit
    May 28, 2019 at 8:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .