Immersion starts with sound and a Singleton — Unity
You’ve been making a game and it is starting to play how you want but there is something missing. For me, that missing part is sound.
Inserting sounds into Unity is fairly simply and they’re easy to use but how to set them up and where to store them in game is dependent on the type of game you’re making.
Say you’re making a 3D game with enemies that could be behind you or to either side, you want the sound to be played where the sound is supposed to be coming from. If a player shoots at you from the right, you should here the sound on the right of you.
You can easily change the setup of your audio source by changing the setting on the component.
Above you can see highlighted the option to change the sound from 2D to 3D and then change the distance that these sounds move through your game world. If you’re making a 3D game it makes sense to store the sounds on the relevant game objects, like the enemy or other players. As you want the sounds to be linked to them but in my case, it made more sense to create a GameData object that stores all of the game audio and in such a way that every script can have access to it.
To start with, I created an empty gameobject called GameData in the mainMenu scene. I then attached all of my game audio and the GameData script. I also gave it a tag of GameData, this will become relevant later on.
I setup the background music audio source to play on awake and to loop. This means that when the game object GameData is loaded in the scene it will automatically start playing the background music.
For all the other audio sources I have these options unticked as I want to play them only when I tell them to.
Next it setting up the GameData class to allow it to be accessible to other scripts and in such a way that there only needs to be one reference to it. I have decided to do it this way because I will now only have one gameobject with an audio source that every enemy can use instead of their being one audio source per enemy. So, if I attached the audio source to the enemy I would be creating extra components that are not needed for my game. So, if I had 30 enemies in the scene at once there is one audiosource instead of 30.
In the start method I am finding all of the gameobjects in the scene with the tag of GameData and if there is more than one I am destroying this one. I am then making sure that my GameData object is not destroyed when a new scene is loaded. Both of the operations make sure that I will only ever have one GameData object in the game.
In the player class is where I am creating the one reference needed for all scripts to have access to the audio sources.
Making the sfx array public and static means that once I have created one reference, I can then access the array through the player class from all my scripts.
What you do have to remember or make note of when doing this, is the order in which you have created your audio sources. I currently have 7 Audio Sources in my GameData object and I have written down the order in which they are shown in the Unity editor. Arrays are zero based so instead of starting from 1 and going to 7, we start at 0 and go to 6.
Above you can see I am playing the 3rd audio source in the array which is the explosion sound and I am accessing it through the Player class without getting another reference to it.
As I said above this works for my game as it is 2D and making the sound play at the right point in the game world is not necessary.
That’s it for this article, thanks for reading and I’ll see you next time.