Master Audio source volume control with slider
Hi all, I am a new game developer. I want to setup an audio source in my starting scene which will be the master volume controller , it will control the sound volume of all the audio source in various scenes of my game. I've made the slider its working on the 'start' screen but after moving to other scenes the volume goes back to the set value in the respective scenes (I've multiple audio sources attached to the main camera of each scene). It looks like I've to use a script to handle this. I've made a C# script which takes one main 'AudioSource' and set each audiosource in all the scenes to the main audiosource. Script goes something like this -
using UnityEngine;
using System.Collections;
public class ChangeVolumeAll : MonoBehaviour
{
AudioSource[] asc;
//private static float vol = GameObject.GetComponents<AudioSource>().volume;
public AudioSource MasterAudioSource;
// Use this for initialization
void Start()
{
asc = GetComponents<AudioSource>();
}
// Update is called once per frame
void Update()
{
SetVolume();
}
public void SetVolume()
{
for (int i = 0; i < asc.Length; i++)
{
asc[i].volume = MasterAudioSource.volume;
}
}
I think this is working fine but the problem is that when some other scene is loaded from the start scene the volume is getting changed to the original set value. Probably this is due to overwriting of volume details after the scene is getting loaded. How can I achieve the result ?
Answer by YourFutureHero · Jan 14, 2016 at 01:08 AM
@shantanusingh994 If you want to set anything up for the player to save you need to use PlayerPrefs or PlayerPrefsManager. I have a "musicManager" which starts at the splash screen to initialize the music. I also have a "levelManager" which calls the level/scene to go to. One other thing, I am using the UI Slider element for my volume control, this is common in mobile development but check out Unity's documentation if you end up using something else to control your volume (i.e. a text field with a number between 1 and 100). I believe this should help as I use it in one of my games but look at the following code setting up an options screen.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class OptionsController : MonoBehaviour {
public Slider volumeSlider;
public Slider difficultySlider;
public LevelManager levelManager;
private MusicManager musicManager;
// Use this for initialization
void Start () {
musicManager = GameObject.FindObjectOfType<MusicManager> ();
volumeSlider.value = PlayerPrefsManager.GetMasterVolume ();
}
// Update is called once per frame
void Update () {
}
public void SaveAndExit () {
PlayerPrefsManager.SetMasterVolume (volumeSlider.value);
levelManager.LoadLevel ("Start Menu");
}
}
If this doesn't work let me know or post your errors and I will attempt to help the best I can.
What is in your PlayerPrefs$$anonymous$$anager i would like to use the code for the OptionsController but i dont have the PlayerPrefs$$anonymous$$anager. Also could you tell me where to put these scripts
I use this as a basic setup for what is needed with the script above. I have a lot more in my stock one for level unlocks and other things. If you are trying to save more than basic options you will want to write to a file.
using UnityEngine;
using System.Collections;
using UnityEngine.Scene$$anonymous$$anagement;
public class PlayerPrefs$$anonymous$$anager : $$anonymous$$onoBehaviour {
const string $$anonymous$$ASTER_VOLU$$anonymous$$E_$$anonymous$$EY = "master_volume";
const string HIGH_SCORE = "High Score";
public static void Set$$anonymous$$asterVolume (float volume) {
if (volume >= 0f && volume <= 1f) {
PlayerPrefs.SetFloat ("master_volume", volume);
} else {
Debug.LogError ("$$anonymous$$aster Volume out of range");
}
}
public static float Get$$anonymous$$asterVolume () {
return PlayerPrefs.GetFloat ($$anonymous$$ASTER_VOLU$$anonymous$$E_$$anonymous$$EY);
}
public static void SetHighScore (int score) {
PlayerPrefs.SetInt (HIGH_SCORE, score);
}
public static int GetHighScore () {
return PlayerPrefs.GetInt (HIGH_SCORE);
}
}
PlayerPrefs$$anonymous$$anager just needs to exist in the project. I use it as the script to consolidate my player preferences. So I know where to look for what PlayerPrefs I have. It's only an organizational technique I like to use. If I ever want to add any PlayerPrefs I can go to the script, see what I already have and then tweak it or add to it, versus looking in various scripts that directly talk to the PlayerPrefs. Does that make sense?
Hope this helps!
Your answer
Follow this Question
Related Questions
Player Movement - Not moving at all 3 Answers
Distance is always zero 0 Answers
Stop AudioSource on Trigger!!! 1 Answer