Home Unity

mute / unmute button

polycounter lvl 7
Offline / Send Message
salimmatta polycounter lvl 7
Hi guys,

Im making a 2D game, but im not a developer but I do have a slight understanding of C#.

However,  any idea how to create a really simple mute / unmute button?

Thanks,

Replies

  • MikeF
    Options
    Offline / Send Message
    MikeF polycounter lvl 19
    I'm assuming you want this for sound or music, if you're looking to mute players in a multiplayer setup its a bit more involved but here goes:
    -create a UI button that will toggle the audio when clicked. 
    -create a C# script with a mute function that the ui button will reference for OnClick() (or whatever the native uGui click method is, i'm only familiar with NGUI)
    -Create a public reference to your audio source inside the C# script

    Once you've got that all set up, just have the mute function call on the audio source, then set the volume to 0 or 1 depending on whether or not you want it muted or un-muted.

    If you have multiple audio sources you'll have to reference all of them within your mute toggle function, or if you're wanting to get more advanced you can assign an audio mixer all your audio sources then directly reference that mixer group volume. See this https://unity3d.com/learn/tutorials/topics/audio/exposed-audiomixer-parameters for more info on that method.

    Lastly another method is to reference your audio listener (by default assigned to your main camera) and change it's volume https://docs.unity3d.com/ScriptReference/AudioListener-volume.html

  • salimmatta
    Options
    Offline / Send Message
    salimmatta polycounter lvl 7
    hey man,

    Thanks for your help, very useful. im sorry I should have mentioned that I need to mute the sound effects not the music. I have taken care of the music part thanks to you. But since im not a developer, i think im a bit confused about how to mute all sound effects..

    Thank you,
  • MikeF
    Options
    Offline / Send Message
    MikeF polycounter lvl 19
    No problem!
    So for the sounds since i'm assuming you've got a ton of audio sources you'l have to use the audio mixer approach. Essentially you'l create the mixer, assign a mixer group to all your audio sources that create a sound effect, then mute that mixer group. Its def. a bit more advanced but i think thats pretty much your best bet
  • salimmatta
    Options
    Offline / Send Message
    salimmatta polycounter lvl 7
    I guess the mixer thing worth a try, even if im a beginner, will it work if I created another one but only for music instead of sound effects?

    Thank you man,
  • MikeF
    Options
    Offline / Send Message
    MikeF polycounter lvl 19
    Yeah when you create an audio mixer you can have different channels, so generally you'l have one for sound, voiceover, music, mic input/output etc..  Every sub group that you create can be drag/dropped into the audio mixer group input of the audio source 
Sign In or Register to comment.