- Home /
Playing Dialogue on Trigger Enter without overlapping sound
Hello all, I'm a novice unity editor and I am trying to set up some game play where when the player enters a space it plays an audio file. I have successfully done this with this code.
// plays audio on collision
var myClip : AudioClip;
function OnTriggerEnter(){
audio.PlayOneShot(myClip);
}
However I'm now running into the problem where the sound will be played over and over again if the player happens to run into the same object. This creates the problem of multiple play backs of dialogue going on at once. As a noob to javascript and unity.... I can't figure out how to set up the if statement.. or something else so that only one playback will be going on at a time.
Your help is much appreciated. Thanks.
Answer by Lost_C4 · Dec 31, 2012 at 06:59 PM
You can add this script to your trigger to play your sound one time in the scene:
function OnTriggerExit (other : Collider) {
if (other.CompareTag ("Player"))
Destroy();
}
I hope it works.
Your answer
Follow this Question
Related Questions
PlayOneShot in OnTriggerEnter function (Unity 5) 0 Answers
how to hide dialogue system until triggered ?? 1 Answer
Play sound when enemy is visible on the screen? 1 Answer
What to do to hear a voice over only 1 time? 2 Answers
How to display a characters "dialogue" when entering a collider? 1 Answer