using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; public class SoundEffectManager : MonoBehaviour { private static SoundEffectManager Instance; private static AudioSource audioSource; private static SoundEffectLibrary soundEffectLibrary; [SerializeField] private Slider sfxSlider; void Awake() { if (Instance == null) { Instance = this; audioSource = GetComponent(); soundEffectLibrary = GetComponent(); DontDestroyOnLoad(gameObject); SceneManager.sceneLoaded += OnSceneLoaded; } else { Destroy(gameObject); } } private void OnSceneLoaded(Scene scene, LoadSceneMode mode) { // Try to find the SFX slider in the new scene (by tag or name for reliability) sfxSlider = GameObject.FindWithTag("SFXSlider")?.GetComponent(); if (sfxSlider != null) { sfxSlider.onValueChanged.RemoveAllListeners(); sfxSlider.onValueChanged.AddListener(delegate { OnValueChange(); }); sfxSlider.value = audioSource.volume; } } public static void Play(string soundName) { AudioClip audioClip = soundEffectLibrary.GetRandomClip(soundName); if (audioClip != null) { audioSource.PlayOneShot(audioClip); } } public static void SetVolume(float volume) { audioSource.volume = volume; } public void OnValueChange() { SetVolume(sfxSlider.value); } }