How does OnTriggerExit works in Unity? Detecting objects leaving an area


In this article we are going to see how the OnTriggerExit method works in Unity. This function will allow us to know when the Collider of a certain GameObject, that was previously inside another Collider, has exit.

OnTriggerEnter and OnTriggerExit are very similar methods but it changes the moment in which the execution of these methods happens, knowing exactly these moments will allow us to build solutions.


The following videos show how to configure the elements in Unity in order to detect objects entering into another object, the video on the left describes all the GameObjects and components in the Unity Editor in order to configure colliders to detect another colliders. In the video on the right we analyze the functions from the Script that are called when a collider enters another collider mark as trigger.

Download Unity Package

Below you can download a Unity package to import it in your project, you will get the Assets we use and you will be able to test it on your own computer.

OnTriggerExit Method in Unity

The OnTriggerExit method is defined in the MonoBehaviour class. That is to say that any new Script by default will have it by inheritance.

Several conditions must be met for OnTriggerExit to run.

The first is that there must be two GameObjects with Colliders involved, one of the Colliders must be in Trigger mode, for this you must check the box “Is Trigger” in the inspector.

In addition, at least one of the GameObjects involved must be assigned a RigidBody component, because OnTriggerEnter is related to the physical part of the engine.

Declare OnTriggerExit

The fact that it is defined in the MonoBehaviour class and a new default Script does not mean that we will automatically have OnTriggerExit in our Script, we must declare it and we do it in the following way:

private void OnTriggerExit(Collider c){

//Acciones a realizar cuando se detecta una entrada al Trigger.


If the conditions are met, this method will run automatically, so inside we write everything that needs to be done when that happens.

The parameter “Collider c” that receives the method, is the Collider of the GameObject that came into contact with the Trigger. We can use this parameter to do many things, for example get the GameObject that has assigned that collider, we do this simply with:


From there we can read any other component assigned to GameObject, its tag, its layer, modify what we need and we are allowed to modify, execute methods defined in Scripts that may be assigned and many more actions.

To learn more about programming functions with parameters I invite you to read this article.


The OnTriggerExit method allows us to detect when a GameObject ceases to be in contact with a Collider in Trigger mode and we can execute the desired actions when that happens.

OnTriggerEnter runs automatically, for this to happen there must be some conditions, the first is that there must be two GameObjects with Colliders involved and one of them in Trigger mode and the second is that at least one of the GameObjects must have a RigidBody component assigned.

Scroll to Top
Secured By miniOrange