How to load audio slider values from PlayerPrefs at the beginning of the game?

Hi everyone!

I have a slight issue with my game , specifically with my audio slider.

The saved audio value is being loaded only when I’m navigating into my menu scenes audio menu (it has the slider childed to it).

The desired function is to load the audio value at the beginning of the game in the start scene but I couldn’t figured it out yet how to do it.

Any help is appreciated! :slight_smile:

Here is the script of my slider:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Audio;

public class MusicSlider : MonoBehaviour {

    public AudioMixer audioMixer;
    public Slider musicSlider;


    public void Start()
    {
        musicSlider.value = PlayerPrefs.GetFloat("Music",1f);
    }



    public void AdjustVolume(float volume)
    {
        PlayerPrefs.SetFloat("Music", volume);

        audioMixer.SetFloat("Music", Mathf.Log10(volume) * 20);
    }

}

Before your music starts, get a volume value from playerprefs. Whenever the player changes the volume during the game set the value in the playerprefs. On game start get the value.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Audio;

 public class MusicSlider : MonoBehaviour {
 
     public AudioMixer audioMixer;
     public Slider musicSlider;
 
 
     public void Start()
     {
         musicSlider.value = PlayerPrefs.GetFloat("Music",1f);
         // Set it at the start        <<<<
         audioMixer.SetFloat("Music", Mathf.Log10(musicSlider.value) * 20);
     }
 
 
 
     public void AdjustVolume(float volume)
     {
         PlayerPrefs.SetFloat("Music", volume);
 
         audioMixer.SetFloat("Music", Mathf.Log10(volume) * 20);
     }
 
 }