Complete the code to declare an AudioSource variable for background music.
private AudioSource [1];The variable backgroundMusic is a clear and descriptive name for the AudioSource that will play the background music.
Complete the code to assign the AudioSource component to the backgroundMusic variable in Start method.
void Start() {
backgroundMusic = GetComponent<[1]>();
}GetComponent<AudioSource>() gets the AudioSource component attached to the same GameObject.
Fix the error in the code to play the background music only once at the start.
void Start() {
backgroundMusic.[1]();
}Play() starts playing the AudioSource clip once from the beginning.
Fill both blanks to create a dictionary that maps scene names to background music clips.
Dictionary<string, AudioClip> musicByScene = new Dictionary<string, AudioClip>() {
{"MainMenu", [1],
{"Level1", [2]
};The dictionary keys are scene names and values are AudioClip variables named accordingly.
Fill all three blanks to set the background music clip and play it if the scene has music.
if (musicByScene.ContainsKey([1])) { backgroundMusic.clip = musicByScene[[2]]; backgroundMusic.[3](); }
We check if the dictionary has the current scene name, then assign the clip and play it.