- Home /
Mute Button
I try to make a mute button on my HUD. So I used the code I found here: http://answers.unity3d.com/questions/15307/i-need-to-make-a-mute-button-solved.html
According to the above link, I add code below to my OnGUI script:
if (M_Mute == false && GUI.Button (Rect(1090,0, 110, 110), PlayTex, GUIStyle.none))
{
AudioListener.pause = true;
M_Mute = true;
}
else if (M_Mute == true && GUI.Button (Rect(1090,0, 110, 110), PlayTex, GUIStyle.none))
{
audio.playOnAwake = false;
M_Mute = false;
}
Well, this button works great when it is hit the first time, and it mutes the sound, but it doesn't replay the audio. I think I have to add this to somewhere else too, so it checks it continuously but I don't know where to put it. thanks for your helps in advanced.
Answer by aldonaletto · Dec 30, 2011 at 12:43 PM
You're confusing several things here:
1- AudioListener is the game "ears" - it hears all sounds and output them to the computer speakers. AudioListener.pause doesn't actually pauses the sound - it mutes/unmutes the game sound;
2- audio.playOnAwake is just an option to start playing the sound as soon as the object owner of this script is created - not all the game sounds, but only the sound attached to this object.
If you want to mute all sounds in your game, you can use this:
if (GUI.Button (Rect(1090,0, 110, 110), PlayTex, GUIStyle.none)) { M_Mute = !M_Mute; AudioListener.pause = M_Mute; }But if you want to pause some specific sound and resume it later, you must place this code in the object owner of the AudioSource you want to stop/resume:
if (GUI.Button (Rect(1090,0, 110, 110), PlayTex, GUIStyle.none)) { audio.Pause(); // toggles pause on/off }NOTE: The rectangle you've specified in GUI.Button has a too high X coordinate, and may not appear in many computers; if you want to align this button to the right side, set the first argument to Screen.width - buttonWidth, like this:
if (GUI.Button(Rect(Screen.width-120, 0, 120, 80), ...)
I wanted to mute the audios so I used the approach you explained in 2. And thanks for your comment about the button coordinates.
Your answer
Follow this Question
Related Questions
Disable all sounds in game 2 Answers
Sound volume on different colisions 1 Answer
Turn off Priority on a Audio Source 1 Answer
Function that does not affect him Time.timeScale? 1 Answer
Audio Source Distance Issues 0 Answers