0
0
Unityframework~20 mins

Background music management in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Background Music Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Unity C# script managing background music volume?
Consider this Unity C# script attached to a GameObject with an AudioSource component. What will be the volume of the AudioSource after Start() runs?
Unity
using UnityEngine;

public class MusicManager : MonoBehaviour
{
    private AudioSource audioSource;

    void Start()
    {
        audioSource = GetComponent<AudioSource>();
        audioSource.volume = 0.5f;
        audioSource.volume += 0.3f;
        audioSource.volume = Mathf.Clamp(audioSource.volume, 0f, 1f);
        Debug.Log(audioSource.volume);
    }
}
A1.0
B0.5
C0.8
D0.3
Attempts:
2 left
💡 Hint
Remember that volume is clamped between 0 and 1 after adding values.
🧠 Conceptual
intermediate
1:30remaining
Which method is best to keep background music playing across multiple scenes in Unity?
You want your background music to continue playing without restarting when switching scenes. Which Unity method helps achieve this?
ADisable the AudioSource component before scene change
BCall Destroy(gameObject) in Awake()
CLoad the music clip again in each scene's Start()
DUse DontDestroyOnLoad(gameObject) on the music GameObject
Attempts:
2 left
💡 Hint
Think about how to keep an object alive between scenes.
🔧 Debug
advanced
2:30remaining
Why does this background music stop playing after scene change?
This script is supposed to keep music playing across scenes, but music stops after loading a new scene. What is the problem?
Unity
using UnityEngine;

public class MusicPlayer : MonoBehaviour
{
    void Awake()
    {
        DontDestroyOnLoad(this);
    }
}
ADontDestroyOnLoad should be called on gameObject, not this
BAwake() is not called before scene change
CAudioSource component is missing, so music can't play
DMusic clip is not assigned in the inspector
Attempts:
2 left
💡 Hint
Check what object is passed to DontDestroyOnLoad.
📝 Syntax
advanced
3:00remaining
Which option correctly fades out background music volume over 3 seconds in Unity?
You want to reduce the volume smoothly to 0 over 3 seconds using a coroutine. Which code snippet is correct?
A
IEnumerator FadeOut() {
  float startVolume = audioSource.volume;
  for (float t = 0; t &lt; 3f; t += Time.deltaTime) {
    audioSource.volume = Mathf.Lerp(startVolume, 0, t / 3f);
    yield return null;
  }
  audioSource.volume = 0;
}
B
IEnumerator FadeOut() {
  float startVolume = audioSource.volume;
  for (float t = 0; t &lt;= 3f; t += Time.deltaTime) {
    audioSource.volume = Mathf.Lerp(startVolume, 0, t / 3f);
    yield return null;
  }
  audioSource.volume = 0;
}
C
IEnumerator FadeOut() {
  float startVolume = audioSource.volume;
  for (float t = 3f; t &gt; 0; t -= Time.deltaTime) {
    audioSource.volume = Mathf.Lerp(startVolume, 0, t / 3f);
    yield return null;
  }
  audioSource.volume = 0;
}
D
IEnumerator FadeOut() {
  float startVolume = audioSource.volume;
  for (float t = 0; t &lt; 3f; t += Time.deltaTime) {
    audioSource.volume = Mathf.Lerp(0, startVolume, t / 3f);
    yield return null;
  }
  audioSource.volume = 0;
}
Attempts:
2 left
💡 Hint
Lerp should go from startVolume to 0 as time increases from 0 to 3.
🚀 Application
expert
3:00remaining
How to implement seamless background music crossfade between two tracks in Unity?
You want to smoothly switch background music from Track A to Track B without silence gap. Which approach is best?
AStop Track A, then start Track B immediately without volume changes
BUse two AudioSources: fade out Track A volume while fading in Track B volume simultaneously
CChange the AudioClip of one AudioSource from Track A to Track B instantly
DPause Track A, wait 1 second, then play Track B
Attempts:
2 left
💡 Hint
Think about overlapping sounds and volume control.