Volume Slider not saving when changing scene
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.
Why are you requiring two AudioSources? That means that the GetComponent might be accessing the wrong AudioSource.
Also how does SaveSliderValue get called? I am assu$$anonymous$$g the slider changes it on value changed. Currently you are never changing the volume variable so you would be saving it to .5f every time. You probably want to crate a method that receives a float and sets the volume.
void SaveSliderValue(float newVolume)
{
volume = newVolume;
PlayerPrefs.SetFloat("SliderBackground", volume);
}
I'll give that a go, cheers.
I pretty much just read through different posts and one of contained that code and said it would work so I thought I'd give it a try.
Answer by ethqnit · Apr 07, 2019 at 07:08 PM
I was able to fix this by doing the following:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
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);
}
}
Your answer
Follow this Question
Related Questions
How Do I Make A Change A Volume With A Slider? 3 Answers
Adjusting Audio Volume via UI Slider 0 Answers
AudioSource not responding to volume change 0 Answers
Adjusting volume of an audio source from a previous scene 1 Answer
Not sure how to word this question but having issue with mixer and sound clips 1 Answer