Question by
Styro8 · Mar 18, 2020 at 10:54 AM ·
programming
Volume Slider not working after changin scenes
I am new here so i don't fully understand coding and other things, so i have problem with volume slider, in main menu i can change it up or down but when i hit play and return to main menu, slider resets and it becomes unusable. Codes:
public class VolumeValueChange : MonoBehaviour {
// Reference to Audio Source component
public AudioSource audioSrc;
// Music volume variable that will be modified
// by dragging slider knob
public float musicVolume = 1f;
void Awake()
{
musicVolume = PlayerPrefs.GetFloat("Volume", musicVolume);
}
// Use this for initialization
void Start()
{
// Assign Audio Source component to control it
audioSrc = GetComponent<AudioSource>();
musicVolume = PlayerPrefs.GetFloat("Volume", musicVolume);
}
// Update is called once per frame
void Update()
{
// Setting volume option of Audio Source to be equal to musicVolume
audioSrc.volume = musicVolume;
}
// Method that is called by slider game object
// This method takes vol value passed by slider
// and sets it as musicValue
public void SetVolume(float vol)
{
musicVolume = vol;
PlayerPrefs.SetFloat("Volume", musicVolume);
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612225206im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
PROBLEM IN MOVING WITH C # 1 Answer
Question about Input.GetKey 1 Answer
Anyone wanna help? 0 Answers
How to have audio play once picked item up? 0 Answers
How do you create a game compatible or friendly for the oculus rift? 0 Answers