OnTriggerExit (141183)

Hi!

I have been trying to make a script so that if the character (cube with a box collider) you control exits a capsule collider (cylinder) it destroys the game object of that character. Its going to be like a arena, if the character exits he falls off the arena and dies (the gameobject is destroyed).

I have this script attached on the arena (with the capsule collider).

using UnityEngine;
using System.Collections;

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

But when the character (cube) exits the collider nothing happens with it.

Please help me, the scripts really seems fine to me.

Cheers!

Please, next time format your code (I just did it for you this time). As for the question, make sure you are working with [colliders marked as triggers][1] and the [layers][2] are interacting with each other. [1]: http://docs.unity3d.com/Manual/CollidersOverview.html [2]: http://docs.unity3d.com/Manual/LayerBasedCollision.html

Does your character contain a Rigidbody?

1 Answer

1

For this to work you need:
1.The gameobject with the script to have a Rigidbody component
2.You need to make sure that the trigger object have isTrigger checked.

And if you want only specific object to be deleted(like your player) you can do:
1.Tag your player with ‘Player’ tag
and then this script will work only if your player is exiting the arena

using UnityEngine;
 using System.Collections;
 
 public class ExampleClass : MonoBehaviour {
     void OnTriggerExit(Collider other) {
         if(other.gameObject.tag == "Player")
         Destroy(other.gameObject);
     }
 }

I will try this out, thanks!

make sure to vote my answer if that worked

Ofcourse!!