0
0
Unityframework~10 mins

Background music management in Unity - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare an AudioSource variable for background music.

Unity
private AudioSource [1];
Drag options to blanks, or click blank then click option'
AbackgroundMusic
BaudioClip
CmusicPlayer
DsoundEffect
Attempts:
3 left
💡 Hint
Common Mistakes
Using a generic name like 'audioClip' which usually refers to the sound file, not the player.
Using names that imply sound effects instead of background music.
2fill in blank
medium

Complete the code to assign the AudioSource component to the backgroundMusic variable in Start method.

Unity
void Start() {
    backgroundMusic = GetComponent<[1]>();
}
Drag options to blanks, or click blank then click option'
AAudioManager
BAudioClip
CAudioListener
DAudioSource
Attempts:
3 left
💡 Hint
Common Mistakes
Using AudioClip instead of AudioSource causes errors because AudioClip is not a component.
Using AudioListener which is for hearing sounds, not playing them.
3fill in blank
hard

Fix the error in the code to play the background music only once at the start.

Unity
void Start() {
    backgroundMusic.[1]();
}
Drag options to blanks, or click blank then click option'
AStop
BPlayOneShot
CPlay
DPause
Attempts:
3 left
💡 Hint
Common Mistakes
Using PlayOneShot causes the clip to play overlapping if called multiple times.
Using Stop or Pause does not start the music.
4fill in blank
hard

Fill both blanks to create a dictionary that maps scene names to background music clips.

Unity
Dictionary<string, AudioClip> musicByScene = new Dictionary<string, AudioClip>() {
    {"MainMenu", [1],
    {"Level1", [2]
};
Drag options to blanks, or click blank then click option'
AmainMenuClip
Blevel1Clip
CmenuMusic
DgameMusic
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up clip names or using unrelated variable names.
Using variable names that do not represent AudioClip objects.
5fill in blank
hard

Fill all three blanks to set the background music clip and play it if the scene has music.

Unity
if (musicByScene.ContainsKey([1])) {
    backgroundMusic.clip = musicByScene[[2]];
    backgroundMusic.[3]();
}
Drag options to blanks, or click blank then click option'
AcurrentScene
CPlay
DStop
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables for the scene name in the dictionary lookup.
Using Stop instead of Play to start the music.