Nerd For Tech
Published in

Nerd For Tech

Unity Guide

Using audio in Unity

A quick guide about how to start using audio in Unity

Objective: Set up a scene in a space shooter game to start using background music with Unity.

In order to show how to start using audio with Unity, we’ll implement background music in a space shooter game.

This is the current state of the space shooter game in Unity.

Listening to audio

As you can see in the next image, an Audio Listener component is attached to the main camera of the scene by default. This component provides a way to receive input from any Audio Source component and then play its sound trough the speakers. There can only be one Audio Listener component per scene.

The Audio Listener component is attached to the main camera by default.

If you want to know more about the Audio Listener component you can visit the Unity docs:

Inputting audio

Now, in order to input audio, let’s create an empty gameobject that will represent the audio manager of the whole scene. Then, as the background music source, create another empty gameobject as its child:

Select the background gameobject and add an Audio Source component in the inspector:

If you want to know more about the Audio Source component you can visit the Unity docs:

Finally, in order to play the music in the game, let’s drag the file from the project folders into the Audio Clip property of the Audio Source component. And, as it’s the background music, let’s make sure that Play on Awake and Loop properties are enabled:

Now every time we run the game, the background music will start playing and it won’t stop once it’s over thanks to the Loop property enabled in the Audio Source component.

And that’s it, you can start using audio in Unity! :D. I’ll see you in the next post, where I’ll be showing how to play sound effects in Unity.

If you want to know more about me, feel free to connect with me on LinkedIn or visit my website :D

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fernando Alcantara Santana

Fernando Alcantara Santana

A passionate computer technology engineer and Unity developer that is always looking to grow in every aspect of life :).