- Home /
 
Audio slider for multiple scenes.
Hello, I'm working on my first game project. I've made the main menu and I've added a slider for the volume. My problem is that the volume slider onlys work for main menu scene and I want to work with every scene but I don't know how to do that. Here's the code so far
 public class VolumeValueChanger : MonoBehaviour
 {
     
     private AudioSource audioSrc;
     
 
     [SerializeField] Slider volumeSlider;
     
     private float musicVolume = 1f;
 
     
     void Start()
     {
 
         
         audioSrc = GetComponent<AudioSource>();
         
     }
     
     void Awake()
     {
         if (PlayerPrefs.HasKey("Volume"))
         {
             SetVolume(PlayerPrefs.GetFloat("Volume"));
             volumeSlider.value = PlayerPrefs.GetFloat("Volume");
         }
     } 
 
     
     void Update()
     {
 
         audioSrc.volume = musicVolume;
         
     }
 
     
     public void SetVolume(float vol)
     {
         musicVolume = vol;
         PlayerPrefs.SetFloat("Volume", vol);
         
     }
 }
 
              Answer by Hellium · Nov 23, 2021 at 08:43 PM
First of all, get rid of your script.
PlayerPrefsFloatLoader.cs
To be attached to any gameObject, preferably to your AudioSource
Add AudioSource > volume (dynamic) to the onValueLoaded event
Add Slider > value (dynamic) to the onValueLoaded event only in the main scene
Set the key to Volume
 using UnityEngine;
 using UnityEngine.Events;
 
 [System.Serializable]
 public class FloatEvent : UnityEvent<float> { }
 
 public class PlayerPrefsFloatLoader : MonoBehaviour
 {
     [SerializeField] private string key;
     [SerializeField] private float defaultValue = 0;
     [SerializeField] private FloatEvent onValueLoaded;
 
     private void Awake()
     {
         onValueLoaded.Invoke(PlayerPrefs.GetFloat(key, defaultValue));
     }
 }
 
               PlayerPrefsFloatSaver.cs
To be attached to any gameObject, preferably to your Slider
Add PlayerPrefsSaver > SetFloat (dynamic) to the onValueChanged event of the Slider in the main scene
Add AudioSource > volume (dynamic) to the onValueChanged event of the Slider in the main scene
Set the key to Volume
 using UnityEngine;
 
 public class PlayerPrefsFloatSaver : MonoBehaviour
 {
     [SerializeField] private string key;
     
     public void SetFloat(float value)
     {
         PlayerPrefs.SetFloat(key, value);
     }
 }
 
               
Right click on image > Open in new tab if it's too small for you to read
Answer by raxashafique · Nov 23, 2021 at 10:08 PM
Since you are writing it in a PlayerPref, and I am assuming that you have another VolumeValueChanger in your other scene as well. Based on the above assumption you can Implement the SceneLoaded Event from Unity SceneManager and re-load the value from PlayerPrefs.
https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager-sceneLoaded.html
P.S. Two quick Solutions:
Make your object Don't Destroy On Load >> In order for it to Persist between scenes and retain its values.
Make a sound manager class, and make it Singleton that also doesn't destroy on load
More info in Don't Destroy On Load here: https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
Your answer