using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using UnityEngine.SceneManagement; public class MenuAudioManager : MonoBehaviour { public static MenuAudioManager Instance; //single instance of MenuAudioManager public Sound[] musicSounds; //array of music sound public AudioSource musicSource; //audio source to play sound private void Update() { if (SceneManager.GetActiveScene().name != "Menu") //If the Scene is not "Menu", destroy MenuAduioManager { Destroy(gameObject); } else if (Instance == null) { // Set the instance to this if no instance exists and make this game object persistent across scenes Instance = this; DontDestroyOnLoad(gameObject); } } private void Start() { PlayMenuMusic("Menu Theme");//play music when the scene is loaded } public void PlayMenuMusic(string name) //playing music by name { Sound s = Array.Find(musicSounds, X => X.name == name); // find sound object in musicSounds array if (s == null) { Debug.Log("Sound Not Found"); //log message if sound isnt found } else { //set clip to found sound and play musicSource.clip = s.clip; musicSource.Play(); } } }