- Home /
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;
}
Answer by Quertie · May 01, 2016 at 01:39 PM
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 :)
I read the docuentation, didn't find what I needed. Will the player prefs store data even after the user quits the application. I am about to use this is for high score, so will the high score save or is there another way to do this
Answer by v01pe_ · Aug 23, 2016 at 10:01 AM
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 AudioSource
s that shall be affected as Output
Open the
Audio Mixer
window and select your Audio MixerSelect the group you want to manipulate ( e.g.`Master` for main volume)
In the Inspector right click on the label
Volume
Click on
Expose 'Volume (of Master)' to script
Rename the
Exposed Parameter
in the dropdown in the top right corner of the Audio Mixer window to e.g.MasterVolume
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.
Answer by Ebonicus · Dec 13, 2016 at 06:04 AM
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:
This allows you to change two game volume options with just one script and two UI sliders.
Ebonicus you refer to a video... I didn't see a link in the thread to a video. I would like to view it, because I tried the method you describe and have not got it to work yet.
Even when you asked years ago, i want to share the answer for folks in the future. The video he mentioned is the Unity tutorial here: https://unity3d.com/de/learn/tutorials/topics/audio/exposed-audiomixer-parameters
I found it, just because of googling one line of code he mentioned. A hint for everybody how to find a source of a published code.
Greetings!
Ebonicus you refer to a video... I have read the whole thread a couple of times and didn't see a link to a video. Which video? I tried the method you describe and so far no luck, so I would like to find that video. Unity is funny that way -- sometimes it seems insanely easy to do something that you would think was really complicated and difficult, other times it seems insanely difficult to do something that you would think would be simple and easy (like presenting the user with volume controls!)...
Don't forget to add: using UnityEngine.Audio; to the monobehaviour.
Your answer
![](https://koobas.hobune.stream/wayback/20220612075309im_/https://answers.unity.com/themes/thub/images/avi.jpg)