Hello guys, got a question. I added music to my block breaker game and while it does go through every scene and plays perfectly it doesn’t use the slider properly, if the player plays the game and comes back to the menu then he can’t change the slide again to increase or decrease volume and they remain with music or no music depending on their choice at the start of the game. Code is this. Any suggestions?
FIRST SCRIPT VOLUME VALUE CHANGER
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UIElements;
using UnityEngine.Audio;
public class VolumeValueChange : MonoBehaviour
{
float volume = 0.5f;
private AudioSource AudioSrc;
private float AudioVolume = 1f;
void Start () {
AudioSrc = GetComponent<AudioSource>();
volume = PlayerPrefs.GetFloat("SliderVolumeLevel", volume);
}
void Update () {
AudioSrc.volume = AudioVolume;
PlayerPrefs.SetFloat("SliderVolumeLevel", volume);
}
public void SetVolume(float vol)
{
AudioVolume = vol;
PlayerPrefs.SetFloat("SliderVolumeLevel", volume);
AudioListener.volume = volume;
}
}
SECOND SCRIPT SOUNDMANAGER
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class SoundManager : MonoBehaviour
{
private static SoundManager instance = null;
public static SoundManager Instance
{
get { return instance; }
}
void Awake()
{
if (instance != null && instance != this)
{
Destroy(this.gameObject);
return;
}
else
{
instance = this;
}
DontDestroyOnLoad(this.gameObject);
}
}