- Home /
OnTriggerEnter Question
I am using the script below to create a trigger zone. I found it online. I have three triggers (cubes, trigger box checked, mesh removed) with this script attached. The scrips are altered to be unique. I designated the fpc to be the collider. The three zones work the first time through. If the user goes back, the triggers to not work. I commented out the part of the script that is supposed to make the trigger only work once. Either way, I get the same result. Each trigger works once. Any help would be appreciated.
var zone1 : Collider; //private var triggered1 : boolean = false;
function OnTriggerEnter(collision : Collider) {
if(collision !=zone1){
return;
}
//triggered1 = true;
print("1B works");
}
Answer by aldonaletto · Jan 22, 2012 at 06:55 PM
It would be better to use tags or names to identify the triggers. If you name them "Trigger1", "Trigger2" and "Trigger3", for instance, you can use this code (player script):
var trig1 = false; var trig2 = false; var trig3 = false;
function OnTriggerEnter(other: Collider){
switch (other.name){ case "Trigger1": trig1 = true; break; case "Trigger2": trig2 = true; break; case "Trigger3": trig3 = true; break; } }
function OnTriggerExit(other: Collider){
switch (other.name){ case "Trigger1": trig1 = false; break; case "Trigger2": trig2 = false; break; case "Trigger3": trig3 = false; break; } } The variables trigN will become true when the player enters the corresponding trigger, and reset to false when the player exits it.
EDITED: I noticed that your script is attached to each trigger, not to the player. In this case, your code could work fine if you added the OnTriggerExit event to reset the triggered variable (the player should be dragged to the zone1 variable of each one).
Hello, Thanks, I was trying to adapt your solution when I saw your edit. It seemed a little more understandable to me. i am a beginner. The latest version is below. i can see that it is reading my exit from the trigger zone, but again it only registers the entrance and exit once. If I go back through nothing happens. Thanks for your help.
var zone1 : Collider; var triggered1 : boolean = false;
function OnTriggerEnter(collision : Collider) {
if(collision !=zone1){
return;
}
triggered1 = true;
print("1B works");
}
function OnTriggerExit(other: Collider){
switch (other.name){ case "coffee_1": triggered1 = false; break; } } //coffee_1 is the tag on the trigger zone
You should add the OnTriggerExit in your own style:
var zone1 : Collider; var triggered1 : boolean = false;
function OnTriggerEnter(collision : Collider) { if (collision == zone1){ triggered1 = true; print("Enter coffee_1"); } }
function OnTriggerExit(collision : Collider) { if (collision == zone1){ triggered1 = false; print("Exit coffee_1"); } } In your case, you must add the script above to each trigger object.
Answer by JJR · Jan 23, 2012 at 04:09 AM
This script is so clean and simple. I put it in. It works for enter and exit for three different trigger zones, but it still won't repeat. Any more suggestions? Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220613063254im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
OnTriggerEnter/Exit called unexpectedly 2 Answers
Checking Tag of Trigger Prefab 1 Answer
Making OnTriggerEnter execute on startup 0 Answers
OnTriggerEnter doesnt see enemy 1 Answer
Check collision of childs Trigger in Parents script [JS] 1 Answer