Ontriggerexit



In this tutorial you will learn how to create and use a box trigger to switch the light color if players enters the trigger area.

1) Open project (TestTestTest), proceed to test scene and go into play mode. 2) In hierarchy select the cube and under its rigidbody component turn 'Is Kinematic' on and off. Actual result: 'DetectionZone' object fires OnTriggerExit/Enter methods. Similarly, when an object leaves LavaVolume’s collider volume, OnTriggerExit will be called. Taking advantage of this functionality, we keep a list of all players who enter the lava. Then, during the Update call, if any players are in the lava, we apply damage to them periodically. OnTriggerExit: TRIGGER STAY 2D: Sent while the game object stays inside a 2D Trigger. OnTriggerStay: PARTICLE COLLISION: Sent when a particle hits a Collider. See unity docs for more info. OnParticleCollision: JOINT BREAK: Sent when a Joint attached to the same game object breaks. OnJointBreak: JOINT BREAK 2D. Posted on December 12, 2013 by alastaira Unity provides the OnTriggerEnter and OnTriggerExit events that fire whenever a collider enters or exits the designated trigger, and also an OnTriggerStay that fires in every frame in which the collider is within the trigger.

Re-enabling a script OnTriggerExit. I have an enemy in my scene that when colliding with a slime gameobject on the ground, will have his script disabled, this works fine. When the slime disappears though, I would like to re-enable his script so he can continue doing his thing. This only works 50% of the time though, the other times, the script.

Ontriggerexit

See Triggers page to learn more about triggers.

1. Create a light

Firstly we need to spawn a point light (it can be other type of light) and place it somewhere in the level near the trigger. So player can see the light color change.

2. Create a collider

Drag and drop the Box Collider from the Toolbox window (Physics section) and adjust it's shape to match the desired area.

3. Check Is Trigger

Created Box Collider will block objects and generate collisions. We want to just get the event from the other objects entering this volume so set the Is Trigger flag to true.

4. Create a script

Next step is to write a C# script (name it TriggerSample). It will handle the trigger event and implement the desired logic. To learn more about creating scripts see this page.

5. Setup a script

Add a script to the created Box Collider and attach a reference to the light (edit LightToControl property).You can also test if it works by using the LightOn checkbox.

6. Test it out!

Hit Play button and enter you player character right into the trigger to see the light changing its color.

In Unity, C# is the main programming language. In our games, we can use every property of C#. However, if you have prior experience with C#, you are probably aware that the structure of a Unity script is different from what you see in C# books. This is because, in Unity, we use scripts that are inherited from the MonoBehaviour class. In this article, we will learn what exactly is MonoBehaviour.

Unity Ontriggerexit Not Working

MonoBehaviour is the base class that every Unity script has to be inherited. A Unity script, that is derived from a MonoBehaviour, serves a bunch of predefined functions(Awake, Start, Update, OnTriggerEnter, etc.) that are executed when an event occurs. This is a design preference that makes game development easier and faster.

Developing a complex game without a game engine is very hard and requires expertise in a lot of fields. Most of the code, which is the same for games, is already developed by Unity engineers including MonoBehaviour class.

Contents

  • MonoBehaviour Methods

Things to consider when writing Unity scripts

Unity

As we mentioned above every Unity script has to be derived from MonoBehaviour class. You cannot add a script to your game objects if it is not derived from MonoBehaviour. If you create your script in the Unity editor, the created script will be ready and derived. Therefore you do not need to worry about that.

Another thing to mention is that you should not use a constructor in a Unity script. It is not prohibited but you may see some behavior that you do not expect. Instead, you should use predefined methods like Awake(), Start() or OnEnable() in order to initialize your scripts.

MonoBehaviour Methods

Methods that come from MonoBehaivour are executed by UnityEngine. MonoBehaviour gives us lots of predefined methods to use for different situations. You can see the complete list of these methods in Unity documentation. Here I will talk about the most used ones.

Awake( )

Awake( ) method is used to initialize the script. You can put initial variables inside the Awake( ), or execute needed tasks at the beginning.

Ontriggerexit2d

This method is called only once during the lifetime of the component whenever execution possible. For instance, if the game object that the script assigned is active at the beginning of the scene, it is executed at the beginning of the scene. If the game object instantiated at the runtime, it is executed when the object is created.

Start( )

Start( ) method is also used to initialize the script as Awake( ) method. However, Start( ) is executed after Awake( ) and before the first call of Update( ). Start( ) is also executed once in its lifetime.

For instance, add the following script to any active game object in your scene, and observe the console.

The result of the script above is the following:

Update( )

Update( ) is called in every frame. This means that if the frame rate of the game is 60 fps, then Update is called 60 times in a second. Since the frame rate of the application may differ from one device to another device, in Update method, you should avoid calling methods that need to consistent by time. For example, physics-related behaviors should not be written in the Update.

Update( ) method is called after Start( ) method. Observe the behavior of the following script.

This will be the result if you execute the script above.

FixedUpdate( )

FixedUpdate( ) runs every fixed time intervals. Therefore, we write the behavior, that we want to execute frame rate independent behavior like physics, into FixedUpdate( ) method.

The time interval between two consecutive FixedUpdate calls is predefined and set to 0.02 seconds. You can change the fixed time intervals from the Time section under the Project Settings.

LateUpdate( )

LateUpdate( ) runs in every frame like Update( ). But it is executed shortly after the Update method is executed. It is generally used for calculations, that need to be done after everything has finished in a frame.

OnEnable( ) and OnDisable( )

OnEnable( ) and OnDisable( ) methods run whenever the script is enabled or disabled, respectively.

OnTriggerEnter( ), OnTriggerStay( ), and OnTriggerExit( )

These methods are only used with a game object that a collider attached. And the collider must be set as a trigger. They are called when a collider interacts with the game object. You can check this article for more information about Trigger and Collision Methods.

OnCollisionEnter( ), OnCollisionStay( ), and OnCollisionExit( )

These methods are similar to Trigger methods but this time collider must not be trigger. They are called when a collider collides with another collider.

The lifecycle of A MonoBehaviour Script

Above we talked about the most used built-in event functions that are in scripts inherit from MonoBehaviour class. Sure, these are only a small portion of the complete list.

It is useful to know the order of execution of these functions. Thus, you may avoid errors that are related to the execution order. For instance, it is not uncommon to try to reach a reference that is still null for those who do not aware of the order of execution.

You do not have to memorize the following flowchart but beware that every built-in event function is not executed randomly instead of in order. You may use the chart as a reference.