Audio problem

Hi there!

I have a question about my paus script. When i press escape the game pauses and the time is set to 0 and all audio stops playing. When i press escape, a button sound needs to play, but if wont because of “AudioListener.volume = 0;”. It plays when i exit the pause menu because it’s set to 1 when i press a second time. I’ve tried to use “yield WaitForSeconds” but it looks weird. Does anyone know how to paus every sound in the level except the button sound?

var paused : boolean = false;
var Target1 : GameObject;
var Target2 : GameObject;
var Target3 : GameObject;
var texture : GUITexture;
var pause : AudioClip;

function Update ()
{

	Pause();

}
	
function Pause()
{

	if(Input.GetKeyDown("escape") && paused == false)
		   {
		   
			AudioSource.PlayClipAtPoint(pause, transform.position);
			//yield WaitForSeconds(0.3);
			paused = true;  
			Time.timeScale = 0;
			savedTimeScale = Time.timeScale;
	    	doPauseWindow = true;
	    	Time.timeScale = 0;
		    AudioListener.volume = 0;
			Target1.GetComponent("MouseLook").enabled = false;
		  	Target2.GetComponent("MouseLookCamera").enabled = false;
		  	Target3.GetComponent("CrosshairSimple").enabled = false;
		  	gameObject.Find("PausetextureBlack").guiTexture.enabled = true;
		  	Screen.showCursor = true;
	 		Screen.lockCursor = false;
	 		texture.enabled = true;
			}
	
		   		else if(Input.GetKeyDown("escape") && paused == true) {
		   
		   			paused = false;
		   			Time.timeScale = 1;
		  			AudioListener.volume = 1;
		  			AudioSource.PlayClipAtPoint(pause, transform.position);
		  			Target1.GetComponent("MouseLook").enabled = true;
		  			Target2.GetComponent("MouseLookCamera").enabled = true;
		  			Target3.GetComponent("CrosshairSimple").enabled = true;
		  			gameObject.Find("PausetextureBlack").guiTexture.enabled = false;
		  			Screen.showCursor = false;
	 				Screen.lockCursor = true;
	 				texture.enabled = false;
	   
	}   
	 
}

The best solution is to set/clear AudioListener.pause instead of controlling the volume: this property is intended to be used exactly in these cases, and you can set the property ignoreListenerPause in the AudioSources that you want to continue playing.

Doing this in your current script requires that you use a dedicated AudioSource, since you don’t have any reference to the temporary AudioSource created by PlayClipAtPoint. Add an AudioSource to the same object that contains the AudioListener (usually the main camera) and set audio.ignoreListenerPause at Start - like this (script attached to the camera):

var paused : boolean = false;
var Target1 : GameObject;
var Target2 : GameObject;
var Target3 : GameObject;
var texture : GUITexture;
var pause : AudioClip;

function Start(){
    audio.ignoreListenerPause = true;
}

function Update()
    // if ESC pressed, toggle pause:
    if(Input.GetKeyDown("escape")) paused = !paused;
        audio.PlayOneShot(pause); // play the pause on/off sound
        if (paused){ // freeze/unfreeze time
            Time.timeScale = 0;
        } else {
            Time.timeScale = 1;
        }
        // set the several variables according to the paused variable:
        doPauseWindow = paused;
        Target1.GetComponent("MouseLook").enabled = !paused;
        Target2.GetComponent("MouseLookCamera").enabled = !paused;
        Target3.GetComponent("CrosshairSimple").enabled = !paused;
        gameObject.Find("PausetextureBlack").guiTexture.enabled = paused;
        Screen.showCursor = paused;
        Screen.lockCursor = !paused;
        texture.enabled = paused;
    }
}