0
0
Unityframework~20 mins

Audio Source component in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Audio Source Mastery
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 AudioSource volume change code?

Consider this Unity C# script snippet that changes the volume of an AudioSource component attached to the same GameObject. What will be the printed volume after running this code?

Unity
using UnityEngine;

public class VolumeTest : MonoBehaviour
{
    void Start()
    {
        AudioSource audio = GetComponent<AudioSource>();
        audio.volume = 0.5f;
        audio.volume += 0.3f;
        Debug.Log($"Volume: {audio.volume}");
    }
}
AVolume: 0.8
BVolume: 0.5
CVolume: 1.0
DVolume: 0.3
Attempts:
2 left
💡 Hint

Remember that volume is a float between 0 and 1 and can be incremented directly.

Predict Output
intermediate
2:00remaining
What happens when you call Play() on an AudioSource without an AudioClip?

Given this Unity C# code, what will be the output or behavior when Play() is called on an AudioSource that has no AudioClip assigned?

Unity
using UnityEngine;

public class PlayTest : MonoBehaviour
{
    void Start()
    {
        AudioSource audio = GetComponent<AudioSource>();
        audio.clip = null;
        audio.Play();
        Debug.Log("Play called");
    }
}
ACompile-time error
BPlay called (no sound plays, no error)
CAudioSource plays default sound
DNullReferenceException at Play()
Attempts:
2 left
💡 Hint

Think about what happens if AudioSource has no clip but Play() is called.

Predict Output
advanced
2:00remaining
What is the output of this spatial blend adjustment code?

Analyze this Unity C# code that modifies the spatialBlend property of an AudioSource. What will be printed?

Unity
using UnityEngine;

public class SpatialBlendTest : MonoBehaviour
{
    void Start()
    {
        AudioSource audio = GetComponent<AudioSource>();
        audio.spatialBlend = 0.0f;
        audio.spatialBlend += 0.7f;
        if (audio.spatialBlend > 1.0f) audio.spatialBlend = 1.0f;
        Debug.Log($"Spatial Blend: {audio.spatialBlend}");
    }
}
ASpatial Blend: 0.7
BSpatial Blend: 1.7
CSpatial Blend: 0.0
DSpatial Blend: 1.0
Attempts:
2 left
💡 Hint

Remember spatialBlend ranges from 0 (2D) to 1 (3D).

Predict Output
advanced
2:00remaining
What error does this code produce when accessing AudioSource.clip length?

Consider this Unity C# code snippet. What error will it produce when run if the AudioSource has no clip assigned?

Unity
using UnityEngine;

public class ClipLengthTest : MonoBehaviour
{
    void Start()
    {
        AudioSource audio = GetComponent<AudioSource>();
        Debug.Log(audio.clip.length);
    }
}
AIndexOutOfRangeException
B0 (zero) printed
CNullReferenceException
DNo output, runs silently
Attempts:
2 left
💡 Hint

What happens if you try to access a property of a null object?

🧠 Conceptual
expert
2:00remaining
How many AudioSource components can you add to a single GameObject in Unity?

In Unity, what is the maximum number of AudioSource components you can add to a single GameObject?

AOnly one AudioSource component per scene is allowed
BExactly two AudioSource components can be added
COnly one AudioSource component is allowed per GameObject
DThere is no fixed limit; you can add multiple AudioSource components
Attempts:
2 left
💡 Hint

Think about Unity's component system and whether it limits multiple components of the same type.