Having some troubles with Audio triggers

I have a basic audio script, and it works fine. I am working on a horror game, so it would be weird to here the same scream by moving through the same location. I would like to make it so that the sound only plays once going through the collider.

Here is my script, sorry I am a noob at Scripting


#pragma strict

var flushClip:AudioClip;
var flush1Clip:AudioClip;
var isFlush = false;
var hasPlayed = false;

function OnTriggerEnter(o:Collider){
Debug.Log(“The trigger fired enter”);
isFlush = true;
hasPlayed = true;

}

function OnTriggerExit(o:Collider){
Debug.Log(“The trigger fired”);

if(isFlush == true) {
playFlush();
}

if(!hasPlayed == true){ //play audio hasPlayed = true;
}

}

function playFlush(){
audio.PlayOneShot(flushClip);
isFlush = false;
hasPlayed = false;
}

function playFlush1(){
audio.PlayOneShot(flushClip);
isFlush = true;
hasPlayed = true;
}


Any assistance would be appreciative. Thank you!

That code is a little confusing. For one the collider for your trigger doesn’t use a tag for the object?

function OnTriggerEnter(o : Collider){

  if (o.tag == "object1"){
         isFlush = true;
         hasPlayed = true;
  }

}

function OnTriggerExit(o : Collider){

  if (o.tag == "object1"  isFlush){
         audio.PlayOneShot(flushClip);
  }

}

Just tag your object as object1 or change up the name to your liking. Also I believe you can just use an array for your audio clips if you have a lot of them.