So I’m very new to Unity C# programming and I’m having a little bit of a problem with the background audio not saving when loading a new scene and then going back to the original scene.
I’ve heard about Singleton and Don’tDestoryOnLoad() but I still don’t fully understand how it works.
The code that I’ve tried I got from multiple Google searches but it still doesn’t work:
[RequireComponent(typeof(AudioSource), typeof(AudioSource))]
public class VolumeSlider : MonoBehaviour
{
public AudioSource source;
float volume = 0.5f;
void Start()
{
source = GetComponent<AudioSource> ();
source.volume = PlayerPrefs.GetFloat ("SliderBackground", source.volume);
}
void SaveSliderValue()
{
PlayerPrefs.SetFloat ("SliderBackground", volume);
}
}
I tried linking the script to the Audio source gameobject and then the Slider gameobject but neither work as intended. Any help on this would be greatly appreciated.