Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Toggle Mute in Unity 4.6

Discussion in 'Audio & Video' started by BrentHeynsmans, Dec 24, 2014.

  1. BrentHeynsmans

    BrentHeynsmans

    Joined:
    Nov 27, 2014
    Posts:
    1
    I'm making my own game in Unity 4.6. I have background music playing all the time. But i want to give my players the possibility to mute the music with a simple toggle. I'm using C#. Anyone any ideas on how to do this?

    Would be a great help!
     
  2. Veemix

    Veemix

    Joined:
    Dec 5, 2014
    Posts:
    25
    If you are playing the music with an AudioSource object you can simply call pause/stop on the AudioSource:

    Code (csharp):
    1.  
    2. audio.Pause();
    3. audio.Stop();
    4.  
     
  3. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,355
    The best method is to disable and Audio Listener component. It's usually located in your camera.

    As a result player will not be able to listen to any kind of audio.
     
    nahjustme likes this.
  4. jerotas

    jerotas

    Joined:
    Sep 4, 2011
    Posts:
    5,572
    That would mean they wouldn't hear SFX though. Not ideal.
     
  5. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,355
    But ideal for disabling all types of audio. Pretty common setting in games.
     
  6. SeasiaInfotechind

    SeasiaInfotechind

    Joined:
    Nov 17, 2014
    Posts:
    32
    Lets do it from New Unity UI.
    Go to the GamObject section, then to the UI section and select the Toggle Button. Button created under a Canvas, Select the Toggle button. There is a toggle script attached to the toggle button. Now you can see there is a OnValueChanged(Boolean), Select the Object on which AUDIO SOURCE is attached. Now, select the audioSource mute function further and you are done.

    You can see in the image one of my audioSource is attached to the camera, therefore first i selected the camera then the audiosource.mute.
     

    Attached Files:

    elmar1028 likes this.