Deactivate Trigger On Exit?

hey i was just wondering if theres a way to deactivate a trigger on trigger exit? I have an objective trigger which shows an introductory to the game but if i step back on it plays all over again. heres the script.

var dooropenandclose : AnimationClip; //this is your animation clip.
var Celldooropensound : AudioClip;
var door : GameObject; //this is your game object that you want to move on trigger.
private var closed = true; // door initially closed
var labelText : String = "";
var ClickSound : AudioClip;

function OnTriggerEnter(){
 audio.PlayOneShot(ClickSound);
 labelText = "This is your cell. You can come back here anytime to save when not on a mission. ";
 yield WaitForSeconds (10);
 audio.PlayOneShot(ClickSound);
 labelText = "To save, go to your bed and press 'E'.";
 yield WaitForSeconds (10);
 labelText = "";
if (closed){ // only do it if door closed
        closed = false; // door now is open
animation.Play ("dooropenandclose");
audio.PlayOneShot(Celldooropensound);

}
}
function OnTriggerExit (other : Collider){
 
 if (other.gameObject.tag == "Player") {
  
  
 }
}
   function OnGUI() {
 GUILayout.Label(labelText);

1 Answer

1

You can use that closed flag to avoid all OnTriggerEnter code - once it gets executed the first time, the flag changes and it will not be executed anymore:

function OnTriggerEnter(){
  if (closed){ // only execute this code if door not open yet
    closed = false; // ensure that this event will never be executed anymore
    audio.PlayOneShot(ClickSound);
    labelText = "This is your cell. You can come back here anytime to save when not on a mission. ";
    yield WaitForSeconds (10);
    audio.PlayOneShot(ClickSound);
    labelText = "To save, go to your bed and press 'E'.";
    yield WaitForSeconds (10);
    labelText = "";
    animation.Play ("dooropenandclose");
    audio.PlayOneShot(Celldooropensound);
  }
}

You don’t even need to mess with OnTriggerExit - the flag is cleared in OnTriggerEnter.