Play Sounds in Unity WITHOUT CODE

Rachel Stadler
5 min readMay 13, 2021

Want to make games? Scared of code? That’s okay, you can get started without it.

Today we’re going to play a sound when the player presses a button. It’s not complicated at all; it takes about four clicks if you know what you’re doing. But there’s a lot of background information to cover before we can get to that, so hold on tight!

This tutorial will assume you have your canvas set up and ready to go. If you don’t know what that means, or how to do it properly, start with this tutorial.

How Unity Deals With Sound

You can put any sound into your game (just make sure you have the rights or it’s copyright free if you intend to make money from ads or selling your game).

You can record a sound with your microphone, like a sound effect or narration. Or, you can download a sound from a website like FreeSound.org (for sound effects) or MusOpen.org (for music).

When you put a sound into Unity, it is called an Audio Clip. Drag the Clip onto a Game Object (your button will do nicely if you have no other plans).

Unity will package it up nicely in an Audio Source, which will be visible as a component in the Inspector.

The Audio Source component holds your sound, and it gives you lots of settings to adjust how your sound is played. You can adjust the volume (how loud), as well as the pitch (how high or low your sound is) and the stereo settings (how much the sound plays in the right speaker or left speaker).

If you ever want to just play background music, you can do that! Just put your music on any active Game Object, and check the box labeled ‘Play On Awake’. This will play the sound immediately when you play the project. If you want the music to play more than once, check the box labeled ‘Loop’. This will play your music over and over again.

But we’re trying to play a single sound when the button is pressed, so we want both of those boxes to be empty right now.

Now that the audio is all ready, let’s put the button together.

Setting Up the Button

Click on the button and find the On Click panel in the Inspector.

Add a function to the list by clicking the plus sign. Put the Game Object with the Audio Source on it into the box currently labeled ‘None’ by dragging it in, or click the little dot next to the box and select the object from the menu that pops up.

Now click on the function menu (currently labeled ‘No Function’), mouse over ‘Audio Source’, and choose ‘Play ()’. (If you don’t see the ‘Audio Source’ option, it means there is no Audio Source on that object. Either you put the wrong object in the function, or you put the Audio Source on the wrong object. Put the right object in the function, or put the Audio Source on the correct object, and you’re good to go!)

And that’s it! Click the play button at the top of your screen to test your work. When you click the button, your sound should play!

Troubleshooting

If your sound plays immediately when the project starts, go into the Audio Source and uncheck the box labeled ‘Play On Awake’.

If your sound plays over and over indefinitely, go into the Audio Source and uncheck the box labeled ‘Loop’.

If the sound plays too loud, or like it’s being played twice simultaneously, you might have a duplicate Audio Source. You can remove a component by finding it in the inspector, clicking the three dots next to the name, and clicking ‘Remove Component’. (And unlike most components, you can just mute Audio Sources by checking the box labeled ‘Mute’.)

Taking it Further

Remember that buttons can be given more than one job.

I would advise against putting more than one sound on the same button, because they will just clash. But you can totally stack a sound effect with an animation trigger, or with an object disappearing or appearing.

And sounds can be anything you want: music, sound effects, even narration. Think outside the box about what sounds you want to include (if any!) and when they should play.

Conclusion

Now you have the tools to make a game, so go do it! You could make a virtual instrument, a storybook that reads to you, or add sound to another type of project. The important thing is that you go and practice your new skills by making something!

But if you’d like to do even more without code, check out the other entries in this series:

Setting Up Buttons WITHOUT CODE

Make Objects Appear or Disappear WITHOUT CODE

Animate and swap sprites WITHOUT CODE

--

--

Rachel Stadler
0 Followers

Likes reading, writing, programming and learning new things.