- Home /
Mute All button
So I'm trying to implement a "Mute All" button - and I'm thinking my best bet is to just turn off the audio listener(though if theres a better way to do it please let me know.) I cant get the audio listener to disable though, I think I'm calling it wrong?
I only have one audio listener in the scene, its attached to a game object called "AudioManager" which I'm using to manage all the game audio. I've tried a number of approaches to get this to work, the most recent of which has been the following code...
var muted : boolean;
var muteText : String;
function Start () {
muted = false;
muteText = "Mute";
}
function OnGUI () {
//this is the code for the button that mutes the game music
if (GUI.Button (Rect (165,420, 70, 40), muteText)) {
if(muted){
muteText = "Mute";
muted = false;
gameObject.Find("AudioManager").GetComponent("Audio Listener").enabled = true;
} else {
muted = true;
muteText = "Unmute";
gameObject.Find("AudioManager").GetComponent("Audio Listener").enabled = false;
}
}
I'm looking over the unity script reference at the moment to try and learn more about audio listeners because this is the first time I've programmatically worked with audio at all. Honestly, I could be totally wrong about my entire approach so feel free to recommend a different way of doing this as opposed to helping me figure out what I'm doing wrong.
Answer by Julien-Lynge · Dec 10, 2013 at 09:13 PM
Why not adjust the volume of the audio listener, rather than trying to disable it? Unity expects an audio listener to always be present, so it will be unhappy if you turn it off.
Try this instead:
http://docs.unity3d.com/Documentation/ScriptReference/AudioListener-volume.html
Your answer