Sounds stack up quickly when you’re building a game. I bet you’re already thinking about audio you’d like in Bobblehead Wars — like a sound effect for when the marine fires his gun, or perhaps a battle cry for the aliens.

You could create AudioClip properties throughout your scripts, but it’s challenging to keep things organized. It’s actually easier to build and use a sound manager as an audio library for your game. You’ll build one in this tutorial.

Note: This tutorial is part of a collection that teaches Unity development from the ground up. You can read the entire series over here. This series is free and does not require any account creation. All assets are provided. If you find it useful, feel free to buy me a coffee.

Building a sound manager

In the Hierarchy, click the Create button and from the drop-down, select Create Empty. Give it the name SoundManager.

This screenshot shows the Hierarchy with the SoundManager GameObject selected.

Click the Add Component button, and from the Audio category, select Audio Source. SoundManager will provide audio clips when asked most of the time, but sometimes it will play sounds directly, too.

Now that you have a sound manager, you should move the background audio source to the sound manager, to keep all the audio in one place. To achieve this, select Main Camera and expand the Audio Source component in the Inspector. Click the settings icon and select Copy Component from the options.

This screenshot shows the AudioSource attached to the Main Camera with the user copying the component.

Select the SoundManager in the Hierarchy and find any component in the Inspector. Click the settings icon and choose Paste Component as New.

This screenshot shows the user pasting the component on the SoundManager.

Finally, return back to the Main Camera. Click the settings icon for the Audio Source, and this time, select Remove Component.

Now you have two audio sources. The first plays sound effects, while the second plays the background music.

Writing the code

Click the Add Component button and select New Script. Name it SoundManager and then click Create and Add. Open the script in your code editor.

You want to be able to access this script anywhere in your game. After the opening brace, add the following:

public static SoundManager Instance = null;  
private AudioSource soundEffectAudio;

Instance will store a static reference to the single instance of SoundManager. This will be handy so you can quickly get a reference to the SoundManager anywhere in your scripts, without having to go through the normal process of making a public variable that you set in the Unity editor.

soundEffectAudio refers to the audio source you added to the SoundManager earlier that will be used to play sound effects. Since you won’t need to adjust the second audio source that plays background music in code, there’s no reason to create a reference for it.

Note: You are building what is known as a singleton. This is an object that can accessed anywhere in your code and there can only be one instance of it. Singletons are a somewhat controversial topic. While they are easy to use, they make your code harder to test and maintain. This is beyond the scope of the topic, but if you are interested, you can read more here.

Next, add the following to Start():

if (Instance == null) 
{
  Instance = this;
} 
else if (Instance != this) 
{
  Destroy(gameObject);
}  

This ensures only one object will exist. If this is the first SoundManager created, it sets the static Instance variable to the current object. If, for some reason, a second SoundManager is created, it automatically destroys itself to make sure that one and only one SoundManager exists.

Next, add the following after the last bit of code:

AudioSource[] sources = GetComponents<AudioSource>();
foreach (AudioSource source in sources) 
{
  if (source.clip == null) {
    soundEffectAudio = source;
  }
}

You need to get a handle to the AudioSource component you added to the SoundManager that you’ll use to play sound, but currently you have two Audio Source components on the GameManager. How can you find the correct one?

Well, one way is to search through all the Audio Source components for the one you’re looking for. GetComponents() returns all of the components of a particular type. Although Unity tends to return components in the same order as they are in the Inspector, you shouldn’t count on that as that behavior may change. Instead, you should look for something that distinguishes the one you’re looking for.

In this case, since you added music to one of the sources, this checks for the audio source that has no music before setting soundEffectAudio.

Creating your library

Now you’re going to build out your sound effects library. Add the following instance variables after the class definition:

[field: SerializeField]
public AudioClip gunFire {get;set;}

[field: SerializeField]
public AudioClip upgradedGunFire {get;set;}

[field: SerializeField]
public AudioClip hurt {get;set;}

[field: SerializeField]
public AudioClip alienDeath {get;set;}

[field: SerializeField]
public AudioClip marineDeath {get;set;}

[field: SerializeField]
public AudioClip victory {get;set;}

[field: SerializeField]
public AudioClip elevatorArrived {get;set;}

[field: SerializeField]
public AudioClip powerUpPickup {get;set;}

[field: SerializeField]
public AudioClip powerUpAppear {get;set;}

Here you defined a series of properties for your various clips. You are using properties annotated with attributes to access them in the editor, but also access them in code.

Save and return to Unity. Select SoundManager in the Hierarchy, open the Sounds folder, and then assign the following sounds to the SoundManager:

  • Gun Fire: bw_shoot
  • Upgraded Gun Fire: bw_triple_shoot
  • Hurt: bw_marine_hurt
  • Alien Death: bw_alien_death
  • Marine Death: bw_marine_death
  • Victory: bw_victory
  • Elevator Arrived: bw_elevator
  • PowerUp Pickup: bw_powerup_pickup
  • PowerUp Appear: bw_powerup_appear

The component should look as follows:

Now you are ready to start adding sound effects to your game.

Where to go from here

You now have a relatively easy sound manager to incorporate sound into your game. This approach was an easy one and you may want to incorporate it into your own game. You also may want individual GameObjects to manage their own sound. Ultimately, it is what works best for your project.

Your next task is to actually integrate your new sound manager. You’ll also a power-up to the game, allowing the space marine to shoot three bullets instead of one. You’ll do this in the next tutorial.


Discover more from Jezner Blog

Subscribe to get the latest posts sent to your email.

By Brian Moakley

Brian Moakley is a writer and editor who lives amongst the quiet hills in New England. When not reading tales of high adventure, he is often telling such stories to all who will listen.

Related Post

Leave a Reply