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!
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!
If you are playing the music with an AudioSource object you can simply call pause/stop on the AudioSource:
audio.Pause();
audio.Stop();
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.
That would mean they wouldn’t hear SFX though. Not ideal.
But ideal for disabling all types of audio. Pretty common setting in games.
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.
