using System; using UnityEngine; using UnityEngine.Audio; public class AudioManager : MonoBehaviour { public Sound[] sounds; public static AudioManager instance; private void Awake() { if (instance == null) instance = this; else { Destroy(gameObject); return; } DontDestroyOnLoad(gameObject); foreach (Sound s in sounds) { s.source = gameObject.AddComponent(); s.source.clip = s.clip; s.source.volume = s.volume; s.source.pitch = s.pitch; s.source.loop = s.loop; } } private void Start() { Play("BackgroundMusic"); } public void Play(string name) { Sound s = Array.Find(sounds, sound => sound.name == name); if (s == null) { Debug.LogWarning("Sound Error: " + name + " could not be played"); return; } s.source.Play(); } }