Question by
yigitoztoprak · May 02, 2021 at 02:54 PM ·
saveslider
Can't save slider value
Hello. I wrote a code to set the sound with the Slider. But this code does not save the values.
What I really want is to be able to adjust the sound in each scene via the pause menu. How can I do it?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SesSlider : MonoBehaviour
{
[SerializeField] Slider volumeSlider;
void Start()
{
if (!PlayerPrefs.HasKey("musicVolume"))
{
PlayerPrefs.SetFloat("musicVolume", 1);
Load();
}
else
{
Load();
}
}
public void ChangeVolume()
{
AudioListener.volume = volumeSlider.value;
Save();
}
private void Load()
{
volumeSlider.value = PlayerPrefs.GetFloat("musicVolume");
}
private void Save()
{
PlayerPrefs.SetFloat("musicVolume", volumeSlider.value);
}
}
Comment
I think you should not change the volume on AudioListener, but on AudioSource that is playing the sound.