🔊 Lesson 6.3: Adding Sound & Music
Sound is half the feel of a game. A cheerful ding when you collect a star, a gentle background loop — both are quick to add in Unity with the Audio Source component.
🎯 Learning Objectives
- Understand Audio Source and Audio Clip
- Play looping background music
- Play a one-shot sound effect on an event (like collecting)
- Know why the Main Camera usually holds an Audio Listener
Estimated Time: 35 minutes
In This Lesson
The Audio Pieces
| Piece | What it is |
|---|---|
| Audio Clip | An imported sound file (.wav, .mp3, .ogg) — an asset in your Project. |
| Audio Source | A component that plays a clip from a GameObject. |
| Audio Listener | The "ears" — one lives on the Main Camera by default. Don't add extras. |
💡 Import a sound by dragging an audio file into the Project panel. It becomes an Audio Clip you can drop into any Audio Source.
Background Music
Music is the simplest case — an Audio Source that plays on start and loops. No code needed:
- Create an empty GameObject named
Music. - Add Component ▸ Audio Source.
- Drag your music clip into the AudioClip field.
- Tick Play On Awake and Loop. Lower the Volume a little (music should sit under the action).
Press Play — music plays and repeats. That's it.
A Collect Sound Effect
Sound effects fire on an event, so we trigger them from code with PlayOneShot. Add an Audio Source to the GameManager (leave Play On Awake off), then:
using UnityEngine;
using TMPro;
public class GameManager : MonoBehaviour
{
public TMP_Text scoreText;
public AudioClip collectSound; // drag a 'ding' clip here
private AudioSource audioSource;
private int score = 0;
void Start()
{
audioSource = GetComponent<AudioSource>();
UpdateScoreText();
}
public void AddPoint()
{
score++;
UpdateScoreText();
// play the collect sound once
if (collectSound != null)
audioSource.PlayOneShot(collectSound);
}
void UpdateScoreText()
{
scoreText.text = "Score: " + score;
}
}
📖 Definition
PlayOneShot(clip): plays a clip once, layered over anything else the source is doing. Ideal for short effects like pickups and clicks — you can fire many rapidly without cutting each other off.
⚠️ No sound?
Check: the GameManager has an Audio Source component; the collectSound clip is assigned; your system volume isn't muted; and there's exactly one Audio Listener (on the Main Camera).
Getting Audio Clips
You need actual sound files. Good sources for free, licensed audio:
- The Unity Asset Store (filter by free) has sound-effect and music packs.
- Sites like freesound.org and opengameart.org offer free clips (always check the licence).
- Short
.wavfiles work great for effects;.oggor.mp3for longer music.
💡 Always check the licence before shipping a game with someone else's audio. "Free to download" isn't always "free to use in a released game" — look for CC0 or clearly permissive licences.
Hands-on Exercise
🏋️ Exercise: Ding!
- Import a short "ding" sound and a looping music track (any free clips).
- Set up looping background music on a
Musicobject (Play On Awake + Loop). - Add an Audio Source to the GameManager and assign the ding to collectSound.
- Play and collect stars — each one dings while the music loops underneath.
💡 Only music plays, no ding
The collectSound clip probably isn't assigned, or the GameManager has no Audio Source. Check both. Also confirm AddPoint() is actually being called (add a Debug.Log).
🎯 Quick Quiz
Question 1: Which method plays a short sound effect once, without interrupting other audio?
Question 2: For looping background music, which two Audio Source options do you tick?
Summary
🎉 Key Takeaways
- An Audio Clip is the sound file; an Audio Source plays it; the Audio Listener (on the camera) hears it.
- Background music = Audio Source with Play On Awake + Loop, no code.
- Sound effects =
audioSource.PlayOneShot(clip)fired from an event. - Use free, correctly-licensed clips; check the licence before releasing.
🚀 What's Next?
You now have every building block — movement, physics, pickups, UI, sound. Module 7 assembles them into a complete, playable mini-game.
🎉 Module 6 complete!
Your game looks and sounds alive. Time to put it all together.