Unity Fundamentals — Adding Sounds to your games

A game without sounds is not a good game. Players will not be immersed in the gameplay you have developed and probably not play it for long. Background music will draw a player into the game with suspense, excitement and more. Sound effects when an event occurs will also help the player know what is going on. Sounds in your game are critical.

Background music

Setting the mood of the game is achieved through the background music. You can change the mood of your game at any time with background music. So let’s add background music to your game. Create a new GameObject called Audio_Manager in your Hierarchy, followed by a child GameObject called Background_Music.

In the Background_Music inspector, add a component called Audio Source. Drag and drop your background music audio file into the Audio Clip section. Set the Audio Source to Play On Awake and Loop.

You now have Background music. Set to play on game startup because the GameObject is in the Hierarchy and is set to Play On Awake.

Sound Effects

A game without sound effects is a very dull game. It is also a way to tell the player that something has occurred. Let’s assign a sound effect to your game. For this article, let’s add an explosion effect to a destruction of a game object.

Find the GameObject that will need the explosion sound effect, and let’s add an Audio Source component to it. We won’t add the audio file to the source as we will add it via the associated script.

In the associated script, we now add the variable for the audio clip and audio source before the start method.

[SerializeField]
private AudioClip _explosionAudio;
private AudioSource _audioSource;

You can have multiple AudioClip variables that can be played to different events. Back in Unity, add the audio file to the AudioClip variable for the script component within the GameObject.

Back at the script file, let’s get the audio source component and assign it to the _audioSource variable. We do this in the void Start method, and it is a good practice to null check that it exists.

_audioSource = GetComponent<AudioSource>();
if (_audioSource == null)
{
Debug.LogError(“Explosion Audio on Enemy is null”);
}

Doing it this way will allow you to assign different sound effect depending on the event on that GameObject.

Find the location of the trigger for the sound effect and assign the correct audio clip to that event, followed by playing the clip.

_audioSource.clip = _explosionAudio;
_audioSource.Play();

This can be interchanged by assigning a different clip to play depending on the event.

Now you can make your game better with music and sound effects. I hope this article helps you create the next addictive game to play.

--

--

--

Unity Developer at Mooski Games

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

HACKGTBIT 2.0 — My Experience

Part 2 — The Application Process

Organization Models for API Management Platforms from an Identity Perspective

Inside Look: Dynamic And Static Libraries

OpenUPM Round-up

The past, present, and possible future of software architecture

Refactoring and its Implementation in ReactJS

How to delete huge data from DynamoDB table?

Delete records using delete-item command & Bulk Deletion of items using batch-write-item API & Updating Time

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
Jose Gil

Jose Gil

Unity Developer at Mooski Games

More from Medium

Installing Unity and Getting Started with Game Development

Making “Eyes” in Unity(Game Dev Day 61)

2.5D Platformer: Creating Collectibles

Unity Camera Movement By Touch similar with Clash of Clans