Want your button to feel extra satisfying?
Let’s add a little audio feedback — a “click!” sound, a coin jingle, or a dramatic BOOM if you’re feeling spicy.
In this lesson, you’ll learn how to:
- Import and use audio clips
- Trigger sounds via UI events
- Control volume and playback in code
🎧 Step 1: Import a Sound Clip
- Find or download a short sound effect (WAV or MP3 works)
- You can use free sites like Kenney.nl or Freesound.org
- Drag the audio file into your
Assets/Audio/
folder - Rename it to something clear like
ClickSound
🎛️ Step 2: Add an AudioSource
- Create an empty GameObject → call it
AudioManager
- With it selected, go to Inspector → Add Component → AudioSource
- In the AudioSource, uncheck:
- Play On Awake (we’ll play it manually)
- Drag your
ClickSound
into the Audio Clip field
🧠 Step 3: Write the Sound Trigger Code
Update your ClickCounter
script:
csharpCopyEditpublic AudioSource clickSound;
public void CountClick()
{
clickCount++;
clickText.text = "Clicks: " + clickCount;
if (clickSound != null)
clickSound.Play();
}
🔗 Step 4: Assign the AudioSource
- Select the GameObject with your
ClickCounter
script - In the Inspector, drag the
AudioManager
(with the AudioSource) into the Click Sound field
Done! Now Unity knows which sound to play.
🧪 Bonus Tips
- Add multiple AudioSources for different sound effects (coin, error, bonus)
- Control volume via
clickSound.volume = 0.5f;
- Use
clickSound.PlayOneShot()
if you want to play overlapping sounds
🧠 Recap
- Imported and organized audio clips
- Used an AudioSource to play sound effects
- Triggered audio through button click via script
- Enhanced game feel and user feedback
🧪 Mini Challenge: Sound Fiesta 🎉
- Add a second sound that plays when player hits 10 clicks
- Use
PlayOneShot()
to prevent audio from being interrupted - Add a mute/unmute toggle button using
audioSource.mute = true/false
🚀 What’s Next?
You’ve got the visuals, the logic, and the sound. Time to bring it all together!
In the next lesson, we’ll finalize and polish your first full game.
🎮 Go to Lesson 17 → Project 1: Build the Full Clicker Game