Adventures in Unity – 1.5 Initial Audio


#1 What would I ideally like to achieve by the time I finish working today – Add initial audio to the game (in game sound effects & background music) – Update GSM to allow the audio to be turned off/on.

#2 What’s the minimum I’d like to achieve by the close of play – Add initial audio to the game (in game sound effects & background music).

There are two parts to adding audio to the game – one part is adding the audio, the other is adding the option to turn off the audio.

When developing I updated the GSM first, then added the audio – But for the point of explaining what I did, I’ll start with the sound effects first.

Adding Audio:

There are quite a few methods for managing audio in Unity. This being my first attempt I wanted to keep things nice and simple. With that in mind, I currently use five sound files – ‘player jump‘, ‘player land‘, ‘background music‘ and a ‘game over tune‘ for in-game sounds & a ‘button down‘ beep for the GSM.

I attached ‘Player jump‘ and ‘player land‘ to the ‘player‘ Unity object – I added two audio sources components to the ‘player‘ object, plugged in the desired mp3 sounds and made sure ‘play on awake‘ was deselected.

I have a script called ‘player.cs‘ attached to the ‘player‘ object – This is used to manager the player – Read user input, update when jumping, etc. To this I added a couple of global audio source variables, initialising them in the Awake() method;

AudioSource[] sounds = GetComponents();
audioJump = sounds[1];
audioLand = sounds[0];

Then when the player jumps, I play the jump sound effect;

if(isJumping && grounded)

And I set up a public method, so when the player collides with a platform- When the OnCollisionEnter method associated with the ‘platform‘ object is called, the’ land sound‘ effect is played;

public void PlayHit()

Background music‘ & ‘game over tune‘ are both handled by the GameController object & associated script.Plugged two audio sources into the GameController object – turned off ‘play on awake‘ but for the background music I turned off ‘loop‘. An attached C# script called GameController which I use primarily to handle the game states was used to manage the audio source’s – Again, like with the player object I created two global variables for each source, I initialised them/plugged them in when Awake() is called. I then created a small method which I invoke to play/stop them as required (at the start of the game and end of the game);

public void InGameMusic(bool doPlay)
     if (doPlay)

That’s pretty much it – I use  completely different approach for the button clock sound effect, which I’ll explain in the GSM section below.

Updating the GSM:

When adding sound effects I thought I’d better update the GSM to allow the player to turn the sound on or off as desired.

This turned out to be a much more involved process than I’d anticipated.

I removed the ‘press space to play‘ message from the GSM and replaced them with three UI button objects ‘play‘, ‘sound on/off‘ & ‘FreeSFX website link

I grabbed the sound effects from a website called ‘‘ – Which provides the sounds free of change so long as they are credited – I added the button since this seemed the easiest way to do so.

For the ‘play‘, ‘sound on/off‘ buttons images, I re-used two pngs I’d created for a number of Android game I developed last year.

FreeSFX website link‘ button – I nabbed from the websites twitter page.


When a player clicks on the ‘play‘ button – It references a method in the ‘GameController.cs‘ script setting a bool called ‘clickPlay‘ to true.

public void ClickPlay()
     clickPlay = true;

In GameController’s update method a check is made to see if the user has pressed/os pressing the space bar, or if ‘clickPlay‘ is true – If either are, it starts the game.

if (Input.GetKeyDown(KeyCode.Space) || clickPlay)

When the user clicks on the ‘sound on/off‘ button – It references a method in the ‘GameController.cs‘ script which switches a bool called ‘clickSound‘ between true & false (play sound, don’t play sound).

public void ClickSound()


     clickSound = !clickSound;




It then runs a second method called ‘SetSound‘ which, depending on  ‘clickSound‘s current value, changes the ‘sound on/off‘ buttons current image & sets the  AudioListener‘s volume respectively.

 public void SetSound()


     if (clickSound)
          buttonSound.image.sprite = soundOnImage;
          AudioListener.volume = 1;
          buttonSound.image.sprite = soundOffImage;
          AudioListener.volume = 0;



When the user clicks on the ‘FreeSFX website link‘ button – It references a method in the ‘GameController.cs‘ script which opens a URL to the freesfx website;

public void ClickFreeSFX()

In the original Android app, when the player touched a ‘button‘, I shrank the image a little; giving the impression it was being pushed by the user’s action. I wanted to replicate this when using the buttons in the game. It took a little while for me to work how best to implement feature – Eventually I settled on creating a small animation to perform the action for me.

This was a surprisingly involved process – Adding ‘Animator‘ element to the GUI Button object, selecting the ‘auto generate animation‘ button, saving the generated animation file to the assets folder, ensure the animation for the desired button state doesn’t loop, then opening the animation window, selecting the desired button state & creating an animation using the timeline – So now the buttons shrink a little when selected.


I also wanted to add a sound effect when the button was clicked – This was much harder to work out than I’d have guessed. Ultimately I implemented the feature by, first adding an audio source element to the button, then adding an ‘EventTrigger‘ script – Which plays the sound effect ‘on pointer Down


That’s about it – I’ll probably switch sound effects in and out as the game develops – I may try being a little more adventurous with the jump noise – Having a sound play throughout the jump, increasing/decreasing the pitch as the play ascends then descends- We’ll see.

For my next step; I’m not sure – I wanted to build an online highscore table – But so far, everything I’ve found online either requires an account (e.g. google play) or needs a mySQL database (unfortunately I’m too poor to afford that level of hosting).

Maybe I’ll start by adding something for the player to pick up during the game – a secondary objective?

Play the game (WebGL)

Grab a copy of the project here


Next post: 1.6 PickUps

Last post: 1.4b Tighten & Tidy

Contents page.

2 Replies to “Adventures in Unity – 1.5 Initial Audio”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: