unity master volume changer 1

unity master volume changer

public float masterVolume;

Void Start { 

  AdjustVolume(masterVolume);

}

public void AdjustVolume (float newVolume) {
     AudioListener.volume = newVolume;
 }

Here is what the above code is Doing:
1. We create a public float variable called masterVolume.
2. In the Start() function, we call the AdjustVolume() function and pass in the masterVolume variable.
3. In the AdjustVolume() function, we set the AudioListener.volume to the newVolume variable.

Now, we can go back to the Unity Editor and set the masterVolume variable to 0.5f.

![](https://github.com/keldavis/c-sharp-practice/blob/master/practice%20projects/unity/unity%20audio%20manager/images/volume%20slider.PNG)

Now, when we run the game, the volume will be set to 0.5f.

![](https://github.com/keldavis/c-sharp-practice/blob/master/practice%20projects/unity/unity%20audio%20manager/images/volume%20slider%20in%20game.PNG)

## Creating a Music Manager

Now, we’re going to create a Music Manager.

First, we’re going to create a new C# script called MusicManager.

Then, we’re going to add the following code to the MusicManager script:

Similar Posts