changing game volume

I have this script here which changes the volume in the main menu but how would I change the volume for the whole game not just the main menu scene. I am not new to unity but I am new to the new unity 5 UI and audio. So if anyone could help that would be great.

public void Volume(float newVolume)
	{
		AudioListener.volume = newVolume;
	}
1 Like

You can also use an Audio Mixer for this. It’s a bit of work to set it up, but once it’s done it’s a very powerful tool.

To change anything, you first have to expose the desired parameter to be able to manipulate it - this can be almost every value from the mixer including effects, etc.

After creating you mixer, you have to assign it to all AudioSources that shall be affected as Output

  1. Open the Audio Mixer window and select your Audio Mixer
  2. Select the group you want to manipulate ( e.g.Master for main volume)
  3. In the Inspector right click on the label Volume
  4. Click on Expose 'Volume (of Master)' to script
  5. Rename the Exposed Parameter in the dropdown in the top right corner of the Audio Mixer window to e.g. MasterVolume
  6. Read/ Write the volume from the script

Example use in a component

public class SetAudioParameter : MonoBehaviour
{
	public AudioMixer mixer;
	public string parameterName = "MasterVolume";

	protected float Parameter
	{
		get
		{
			float parameter;
			mixer.GetFloat(parameterName, out parameter);
			return parameter;
		}
		set
		{
			mixer.SetFloat(parameterName, value);
		}
	}
}

Of course, you first have to drag in your AudioMixer to the Component’s field in the editor, to make it work.

Perhaps you could use the PlayerPrefs to store the newVolume float, since PlayerPrefs are saved between sessions (and, a fortiori, also between scenes!)

So, when you change the volume:

using UnityEngine;
using System.Collections;

public class Volume: MonoBehaviour
{
    public void changeVolume(float newVolume)
    {
        PlayerPrefs.SetFloat("volume", newVolume);
        AudioListener.volume = PlayerPrefs.GetFloat("volume");
    }
}

On every scene, you can then add:

using UnityEngine;
using System.Collections;

public class GameController : MonoBehaviour
{
    private void Start()
    {
        AudioListener.volume = PlayerPrefs.GetFloat("volume");
    }
}

Hope this helps :slight_smile:

The video actually offers a very slick method of doing this, and uses a very simple single script that is public and attached to some canvas or gameobject so the functions are available publicly.

public class MixLevels : MonoBehaviour {

	public AudioMixer masterMixer;


	public void SetSfxLevel(float sfxLvl) {
	
		masterMixer.SetFloat ("volSfx", sfxLvl);
	
	}

	public void SetMusicLevel(float musicLvl) {

		masterMixer.SetFloat ("volMusic", musicLvl);

	}


}

Then you just have two sliders, and they just call the functions directly from the inspector settings of the sliders:

83955-capture.jpg

This allows you to change two game volume options with just one script and two UI sliders.