Challenge - 5 Problems
Background Music Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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); } }
Attempts:
2 left
💡 Hint
Remember that volume is clamped between 0 and 1 after adding values.
✗ Incorrect
The volume starts at 0.5, then 0.3 is added making 0.8. Clamping keeps it at 0.8 because it's within 0 and 1.
🧠 Conceptual
intermediate1: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?
Attempts:
2 left
💡 Hint
Think about how to keep an object alive between scenes.
✗ Incorrect
DontDestroyOnLoad keeps the GameObject alive when scenes change, so music continues without restarting.
🔧 Debug
advanced2: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);
}
}Attempts:
2 left
💡 Hint
Check what object is passed to DontDestroyOnLoad.
✗ Incorrect
DontDestroyOnLoad(this) only protects the script instance, but the GameObject and its components can be destroyed. Use DontDestroyOnLoad(gameObject) to keep the whole object alive.
📝 Syntax
advanced3: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?
Attempts:
2 left
💡 Hint
Lerp should go from startVolume to 0 as time increases from 0 to 3.
✗ Incorrect
Option A correctly increases t from 0 to 3, interpolating volume from startVolume down to 0. Option A includes t=3 which is okay but less common. Option A counts down t which reverses the fade. Option A reverses start and end values in Lerp.
🚀 Application
expert3: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?
Attempts:
2 left
💡 Hint
Think about overlapping sounds and volume control.
✗ Incorrect
Using two AudioSources allows one track to fade out while the other fades in, creating a smooth crossfade without silence.