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


In this arti¬≠cle we are going to see how the OnTrig¬≠gerEx¬≠it method works in Uni¬≠ty. This func¬≠tion will allow us to know when the Col¬≠lid¬≠er of a cer¬≠tain GameOb¬≠ject, that was pre¬≠vi¬≠ous¬≠ly inside anoth¬≠er Col¬≠lid¬≠er, has exit. 

OnTrig¬≠ger¬≠Enter and OnTrig¬≠gerEx¬≠it are very sim¬≠i¬≠lar meth¬≠ods but it changes the moment in which the exe¬≠cu¬≠tion of these meth¬≠ods hap¬≠pens, know¬≠ing exact¬≠ly these moments will allow us to build solutions. 

Here are two videos that ana­lyze a pro­to­type using OnTrig­ger­Enter and OnTrig­gerEx­it to exe­cute actions when an object enters a trig­ger Col­lid­er. You can down­load the Uni­ty pack­age in this arti­cle.


The fol­low­ing videos show how to con­fig­ure the ele­ments in Uni­ty in order to detect objects enter­ing into anoth­er object, the video on the left describes all the GameOb­jects and com­po­nents in the Uni­ty Edi­tor in order to con­fig­ure col­lid­ers to detect anoth­er col­lid­ers. In the video on the right we ana­lyze the func­tions from the Script that are called when a col­lid­er enters anoth­er col­lid­er mark as trigger.


OnTriggerExit Method in Unity

The OnTrig­gerEx­it method is defined in the MonoBe­hav­iour class. That is to say that any new Script by default will have it by inheritance.

Sev¬≠er¬≠al con¬≠di¬≠tions must be met for OnTrig¬≠gerEx¬≠it to run. 

The first is that there must be two GameOb­jects with Col­lid­ers involved, one of the Col­lid­ers must be in Trig­ger mode, for this you must check the box "Is Trig­ger" in the inspector.

In addi­tion, at least one of the GameOb­jects involved must be assigned a Rigid­Body com­po­nent, because OnTrig­ger­Enter is relat­ed to the phys­i­cal part of the engine.

Declare OnTriggerExit

The fact that it is defined in the MonoBe¬≠hav¬≠iour class and a new default Script does not mean that we will auto¬≠mat¬≠i¬≠cal¬≠ly have OnTrig¬≠gerEx¬≠it in our Script, we must declare it and we do it in the fol¬≠low¬≠ing way:

private void OnTriggerExit(Collider c){

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


If the con­di­tions are met, this method will run auto­mat­i­cal­ly, so inside we write every­thing that needs to be done when that happens.

The para¬≠me¬≠ter "Col¬≠lid¬≠er c" that receives the method, is the Col¬≠lid¬≠er of the GameOb¬≠ject that came into con¬≠tact with the Trig¬≠ger. We can use this para¬≠me¬≠ter to do many things, for exam¬≠ple get the GameOb¬≠ject that has assigned that col¬≠lid¬≠er, we do this sim¬≠ply with:


From there we can read any oth­er com­po­nent assigned to GameOb­ject, its tag, its lay­er, mod­i­fy what we need and we are allowed to mod­i­fy, exe­cute meth­ods defined in Scripts that may be assigned and many more actions.

To learn more about pro­gram­ming func­tions with para­me­ters I invite you to read this arti­cle.


The OnTrig­gerEx­it method allows us to detect when a GameOb­ject ceas­es to be in con­tact with a Col­lid­er in Trig­ger mode and we can exe­cute the desired actions when that happens.

OnTrig­ger­Enter runs auto­mat­i­cal­ly, for this to hap­pen there must be some con­di­tions, the first is that there must be two GameOb­jects with Col­lid­ers involved and one of them in Trig­ger mode and the sec­ond is that at least one of the GameOb­jects must have a Rigid­Body com­po­nent assigned.


Scroll to Top
Secured By miniOrange