Objects not being destroyed

I looked around the forums and keep getting pointers back to the same solution I’m trying.

I’m spawning prefabs (simple cubes w/ box colliders) then applying a transform to them to have them come at the camera like so:

void Update()
{
    transform.position += Time.deltaTime * transform.forward * 3;
}

then, when they pass behind the field of view, they hit a mesh assigned with this script:

public class DestroyObjects : MonoBehaviour
{
    void OnTriggerExit(Collider other)
    {
        Destroy(other.gameObject);
    }
}

yet they aren’t being destroyed. Is Trigger ☑ is enabled on the mesh with the destroy script.

Any thoughts, or can I provide more information?

Thanks in advance.

Following up on this, I have tried a couple of tests including:

  • creating a simple scene with two cubes (one trigger and one destroyer) set to a desktop publishing profile (the original project was targeting an Oculus)
  • removed the movement script from the objects I want to destroy and made them mouse-movable

still not able to destroy anything.

Any thoughts are appreciated. Let me know if there is more information I can provide.
Thanks!

Did you ever figure out a solution for this? I’m experiencing a very similar problem in my code currently.

Try OnTiggerEnter instead of OnTiggerExit

The moving object needs to have a rigidbody component attached or you won’t get any trigger callbacks. Triggers and colliders are part of the physics system. Any moving collider should have a rigidbody component or be part of a rigidbody (i.e. being a child of a rigidbody that actually moves). If the object is moved solely though scripting, you should mark the rigidbody as a “kinematic” rigidbody. That way it will not be influenced by external forces like gravity or collisions but it can still push non-kinematic rigidbodies or cause trigger callbacks.