Audio 'multiplying' when using audio.PlayOneShot()

Hey all,

Ive got a bit of a problem, when the audio plays in this script the audio ‘multiplies’ or plays many times over itself.

Heres the code:

        var camera1 : Camera;
        var camera2 : Camera;
        var camera3 : Camera;
        var otherFPS : GameObject;
        var fadeTexture : Texture2D;
     var fadeSpeed = 0.2;
     var drawDepth = -1000; 
     private var alpha = 0.0; 
     private var fadeDir = -1;
     private var guiShow : boolean = false;
     private var suspense : boolean = false;
       var Audio1 : AudioClip;
       var Audio2 : AudioClip;
      function Start () {
        camera1.GetComponent.<Camera>().enabled = true;
              camera2.GetComponent.<Camera>().enabled = false;  
              camera3.GetComponent.<Camera>().enabled = false;    
              yield WaitForSeconds(2);         
              camera1.GetComponent.<Camera>().enabled = false;
              camera2.GetComponent.<Camera>().enabled = true;
    function OnTriggerStay (Col : Collider)
     if(Col.tag == "Player")
              camera2.GetComponent.<Camera>().enabled = false;
              camera3.GetComponent.<Camera>().enabled = true;
        yield WaitForSeconds (4);
        yield WaitForSeconds (14);
        suspense = true;
        yield WaitForSeconds (8);
        yield WaitForSeconds (16);
        guiShow = true;
        yield WaitForSeconds (1);
    function OnGUI()
     if(guiShow == true)
          alpha -= fadeDir  fadeSpeed  Time.deltaTime;  
           alpha = Mathf.Clamp01(alpha);   
           GUI.color.a = alpha;
             GUI.depth = drawDepth;
             GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeTexture);
    function Update()
    if(suspense == true)



To extend 12boulla’s answer, this happens because you use .PlayOneShot in OnTriggerStay().
The sound will be played once for each frame the object is touching/inside the trigger.

Theoretically this means that at 60 fps if the object is inside the trigger for exactly 1 second, it will be started 60 times within that second.

If you want to play the sound as soon as the trigger is touched, put the code inside OnTriggerEnter(), if you want it to play x seconds after entering the trigger, invoke a new function for that x seconds and put the .PlayOneShot part inside that.

You could also use a bool inside OnTriggerStay() and check if it is true/false.

OnTriggerStay is called once per frame for every Collider other that is touching the trigger.

So if there is a collider touching the trigger with the script, OnTriggerStay() will be executed once per frame, and so there will be issues with the audio.

Unity Documentation of OnTriggerStay()