x


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.

more ▼

asked Dec 30 '11 at 12:10 PM

sanamdehghan gravatar image

sanamdehghan
46 14 21 23

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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), ...)
more ▼

answered Dec 30 '11 at 12:43 PM

aldonaletto gravatar image

aldonaletto
41.5k 16 42 197

I wanted to mute the audios so I used the approach you explained in 2. And thanks for your comment about the button coordinates.

Dec 30 '11 at 01:23 PM sanamdehghan
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x18
x10

asked: Dec 30 '11 at 12:10 PM

Seen: 1335 times

Last Updated: Dec 30 '11 at 01:23 PM