How to control master volume for all scenes using slider in option,Script for adjusting volume using slider
Hi, Can anyone help me with this, I am just learning unity's basics and now I am trying to use slider to be the master volume to control all the scenes volume. I am currently using this basic code placed in the MusicManager/Audio source to control volume but only limited to a scene in which it is used and also it doesn't save the position on where it is last placed. Any help would be appreciated thanks.
public Slider Volume;
public AudioSource myMusic;
// Update is called once per frame
void Update () {
myMusic.volume = Volume.value;
}
}
Answer by tormentoarmagedoom · Sep 17, 2018 at 11:38 AM
Good day.
You are asking different things.
First.
You need to link in every scene the slider with the Audiosource. You can create a code to automatic do it, for example, make all audiosource be in an object with a specific tag like "MusicPlayer", and use
foreach (GameObject AudSou in GameObject.FindObjectsWithTag("MusicPlayer"))
{
AudSou.GetComponent<AudioSource>().volume = Volume.value;
}
So all Audiosource will set its volume. But of course, you need to do this in all scene.
This is only one way, there are so many.... You can do a script for each audiosource to be always sync with the value of the slider.
Then you ask to "save" the volume between scenes and/or between instances of the game (when you close the game and open it in other moment).
YOu need to learn about data persistance. Look at the Persistance Unity tutorial, it can seem long, but if you want to continue your adventure in game developing, you need to learn it, its 100% basic.
Good luck!
Your answer
Follow this Question
Related Questions
I need help with my scripts! 1 Answer
wait for a button to continue a loop,Inside a loop of turns i want to put "PassTurnButton" 0 Answers
Audio play when enter trigger 1 Answer
Restarting after pressing a key 0 Answers
How would you move from a array like from [2] to [3] or backwards like [2] to [1] 2 Answers