Introduction
In this article we are going to study how to use the OnTriggerStay method to detect the character in a certain region and apply actions when that happens. Specifically, we’re going to make the damage and health regeneration stations affect the character.
Go to the project’s main page
Procedure
We are going to use the “OnTriggerStay” station, which consists of two metal-detector-shaped devices, one red and one green, whose function is to affect our health system. When entering the red station our health should decrease and with the green station it should increase.
![3d models similar to metal detector. In the center a sign that says "OnTriggerStay Station".](https://gamedevtraum.com/wp-content/uploads/post/desarrolloJuegos/unityFundamentals/OnTriggerStay/3.webp)
All these objects are inside the empty GameObject “#5 OnTriggerStay“. We have a regeneration station (HealthStation) and a damage station (DamageStation).
![the "GameDevLab" scene hierarchy of a Unity3D project.](https://gamedevtraum.com/wp-content/uploads/post/desarrolloJuegos/unityFundamentals/OnTriggerStay/crear-juegos-en-unity-5-inspector.webp)
Both HealthStation and DamageStation are assigned the same script, “OnTriggerStayAction“.
![component script "OnTriggerStay Action" displayed in inspector in Unity3D](https://gamedevtraum.com/wp-content/uploads/post/desarrolloJuegos/unityFundamentals/OnTriggerStay/crear-juegos-en-unity-5-script-ontriggerstayaction.webp)
In the inspector we can modify the amount of health that gives or takes away through the float “amountHealth” and we can choose the type of station that is.
![component script "OnTriggerStay Action" displayed in inspector in Unity3D. Drop-down menu with health and damage options](https://gamedevtraum.com/wp-content/uploads/post/desarrolloJuegos/unityFundamentals/OnTriggerStay/2.webp)
In order for the stations to be able to do their work, it is necessary for the player to have some kind of health system to affect. This is already solved in the GameDevLab. The GameObject FirstPersonPlayer is assigned a script called “HealthSimple” that has the necessary variables and methods to provide the player with a simple health system.
![component script "HealthSimple" displayed in inspector in Unity3D](https://gamedevtraum.com/wp-content/uploads/post/desarrolloJuegos/unityFundamentals/OnTriggerStay/crear-juegos-en-unity-5-script-healthsimple.webp)
Resolution
When opening the OnTriggerStayAction script for the first time we find what you see in figure 6. There is a defined region between comments where it is suggested to redefine the OnTriggerStay method.
![c# script in unity](https://gamedevtraum.com/wp-content/uploads/post/desarrolloJuegos/unityFundamentals/OnTriggerStay/crear-juegos-en-unity-5-script-sin-completar.webp)
How does the OnTriggerStay method work?
This method is defined in the MonoBehaviour class, so it will be present by default in any new script we make.
Runs automatically if a GameObject with Collider enters the region defined by another Collider who is in Trigger Mode. In addition, at least one of the two GameObjects must have a RigidBody component assigned to it.
![](https://gamedevtraum.com/wp-content/uploads/post/desarrolloJuegos/unityFundamentals/OnTriggerStay/crear-juegos-en-unity-5-interaccion-entre-colliders-1.webp)
![](https://gamedevtraum.com/wp-content/uploads/post/desarrolloJuegos/unityFundamentals/OnTriggerStay/crear-juegos-en-unity-5-interaccion-entre-colliders-2.webp)
Figure 9 shows the resolution for the exercise.
![](https://gamedevtraum.com/wp-content/uploads/post/desarrolloJuegos/unityFundamentals/OnTriggerStay/1.webp)
We redefine the OnTriggerStay method that receives as parameter the Collider that has come in contact with the trigger. We give the name “col” to the reference of this Collider.
What we have to do is first determine if the GameObject whose Collider came into contact has a health system to affect, we do this by getting the reference of the HealthSimple component that can be assigned to this GameObject.
If the GameObject has health system, we will have a component HealthSimple, otherwise our variable will have the value null. We use this as a condition in the if statement.
If the condition is true we execute the playerIsUnderTheStation method and pass as parameter the HealthSimple reference found in health.
With this we have solved the problem of video, from that point the component HealthSimple will handle the issue of health.
![room with several 3d models, vending machine, gears, in the middle there is a sign that says "don't forget to subscribe".](https://gamedevtraum.com/wp-content/uploads/post/desarrolloJuegos/unityFundamentals/OnTriggerStay/4.webp)
Upon entering the damage station our health begins to decline, if it reaches zero value the scene is restarted.
Upon entering the regeneration station, health begins to increase until the maximum value is reached.
![room with several 3d models in the center you see a sign that says "don't forget to subscribe".](https://gamedevtraum.com/wp-content/uploads/post/desarrolloJuegos/unityFundamentals/OnTriggerStay/5.webp)
Conclusion
The objective of this article and corresponding video is to study an application of the OnTriggerStay method, to understand how it works and then be able to apply it to any situation in which it may be useful.
There are many situations in which this method could serve, for example modeling the behavior of a pressure plate, make us affect an opposite force to gravity that makes us levitate if we are in a certain region. In short, this method is a useful tool that we have to understand.
It is not the point of this article to study the health system, but it is interesting to note that the stations are going to act on any GameObject that has the HealthSimple script, regardless of the player. This suggests that in our game we could have a single script that manages the health of any living being in our game and has properties common to all, as well as unique properties for each being. Creating this kind of solutions is what I find most interesting