Bird
Raised Fist0
Unityframework~20 mins

Background music management in Unity - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of using DontDestroyOnLoad with background music in Unity?
easy
A. To stop the music when a new scene loads
B. To pause the music when the game is minimized
C. To change the music volume automatically
D. To keep the music playing continuously across different scenes

Solution

  1. Step 1: Understand the role of DontDestroyOnLoad

    This function prevents the GameObject from being destroyed when loading a new scene.
  2. Step 2: Apply this to background music

    By using DontDestroyOnLoad on the music GameObject, the music keeps playing without restarting or stopping between scenes.
  3. Final Answer:

    To keep the music playing continuously across different scenes -> Option D
  4. Quick Check:

    DontDestroyOnLoad keeps objects alive across scenes [OK]
Hint: Remember: DontDestroyOnLoad keeps music playing between scenes [OK]
Common Mistakes:
  • Thinking it stops music on scene change
  • Confusing it with volume control
  • Assuming it pauses music automatically
2. Which of the following is the correct way to play background music using an AudioSource component in Unity?
easy
A. audioSource.Play();
B. audioSource.Start();
C. audioSource.Begin();
D. audioSource.Run();

Solution

  1. Step 1: Recall AudioSource methods

    The AudioSource component uses Play() to start playing audio clips.
  2. Step 2: Identify the correct method

    Among the options, only Play() is a valid AudioSource method to play sound.
  3. Final Answer:

    audioSource.Play(); -> Option A
  4. Quick Check:

    AudioSource.Play() starts audio playback [OK]
Hint: Use Play() to start audio on AudioSource [OK]
Common Mistakes:
  • Using non-existent methods like Start() or Run()
  • Confusing Play() with Pause() or Stop()
  • Forgetting to assign an AudioClip before playing
3. What will be the output of the following Unity C# code snippet?
AudioSource audioSource = gameObject.AddComponent<AudioSource>();
audioSource.clip = backgroundMusicClip;
audioSource.volume = 0.5f;
audioSource.Play();
Debug.Log(audioSource.isPlaying);
medium
A. False
B. True
C. NullReferenceException
D. Compilation error

Solution

  1. Step 1: Analyze AudioSource setup

    The code adds an AudioSource, assigns a clip, sets volume, and calls Play(), so audio starts playing.
  2. Step 2: Check isPlaying property

    isPlaying returns true if the audio is currently playing, which it is after Play() is called.
  3. Final Answer:

    True -> Option B
  4. Quick Check:

    AudioSource.isPlaying is true after Play() [OK]
Hint: After Play(), isPlaying returns true while audio plays [OK]
Common Mistakes:
  • Assuming isPlaying is false immediately after Play()
  • Confusing volume with playback state
  • Expecting errors without assigning AudioClip
4. Identify the error in this Unity C# script snippet for background music management:
void Start() {
    AudioSource audioSource = GetComponent<AudioSource>();
    audioSource.clip = backgroundMusicClip;
    audioSource.Play;
}
medium
A. Missing parentheses after Play method call
B. AudioSource component is not added
C. backgroundMusicClip is not assigned
D. GetComponent<AudioSource>() returns null

Solution

  1. Step 1: Check method call syntax

    The line audioSource.Play; is missing parentheses, so it does not call the Play method.
  2. Step 2: Understand method invocation

    Methods require parentheses even if no arguments are passed, so it should be audioSource.Play();.
  3. Final Answer:

    Missing parentheses after Play method call -> Option A
  4. Quick Check:

    Method calls need () even if empty [OK]
Hint: Always add () to call methods like Play() [OK]
Common Mistakes:
  • Forgetting parentheses on method calls
  • Assuming Play is a property, not a method
  • Ignoring compiler errors from missing ()
5. You want to create a background music manager in Unity that plays music continuously across scenes without duplicates. Which approach is best?
hard
A. Reload the music clip every time a scene loads without preserving the AudioSource
B. Add a new AudioSource with Play() in every scene's Start method
C. Use a singleton pattern with DontDestroyOnLoad and check if an instance exists before creating a new one
D. Use Stop() on AudioSource in each scene and start new music

Solution

  1. Step 1: Understand the problem of duplicates

    Without control, multiple music objects can play simultaneously when scenes change.
  2. Step 2: Use singleton with DontDestroyOnLoad

    A singleton ensures only one music manager exists. Using DontDestroyOnLoad keeps it alive across scenes, preventing duplicates.
  3. Final Answer:

    Use a singleton pattern with DontDestroyOnLoad and check if an instance exists before creating a new one -> Option C
  4. Quick Check:

    Singleton + DontDestroyOnLoad avoids duplicate music [OK]
Hint: Singleton + DontDestroyOnLoad prevents duplicate music [OK]
Common Mistakes:
  • Creating new AudioSource each scene causing overlap
  • Stopping music unnecessarily on scene load
  • Not checking for existing music manager instance