- Home /
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;
}
}
Answer by aldonaletto · Sep 04, 2013 at 01:00 PM
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;
}
}
When I try this and test the level the sound is playing all the time. And I get an error "Field 'UnityEngine.AudioSource.ignoreListenerPause' not found."
@RiseX, what is your unity3d version? I think that it is a new property released in 4.1.
Whatever, appear to have some bugs in AudioSource static variables. If your solution is working now, try to stay there until you have better alternatives. If not , like @aldonaletto said, you need to play your audio in different AudioSource, one for runtime where you set volume to 0 when paused, one for audios that play paused.
@Sisso is right: this is a new feature, not available in Unity 3.x
Your answer
Follow this Question
Related Questions
Pause Audio Help 1 Answer
Pause script errror. 1 Answer
Is it possible to query the Unity (3.0) transform tree from a secondary thread? 1 Answer
Play audio on keyboard click? 1 Answer
How do I gaplessly change music clips in a script? 2 Answers