Game Audio | Basic Programming - Part 1 | Unity & C♯

  |  Pyramind  |  

In this first part of our multi part series to basic programming for audio, we are diving into simply playing a sound in the game. There are a lot of terms and ideas thrown out that aren’t explained fully in the video, so below is a simple guide to these terms. There is a lot to unpack here, so be sure to stop the video and read about each one, as they are all extremely important to know if you are going to be doing even basic programming. Programming is somewhat like a puzzle; it's hard to see how all the pieces fit together at first, but once you do start putting the right pieces in place you start to see a clearer picture.

Download Link to Unity Projects:


You can download two Unity projects - a Master project, with everything finished, and a blank project to follow along with what Brennan is doing. You can even import your own sounds into the demo to make it sound how you like.

Audio Manager Class: The idea here is that we want a centralized place to control how the audio is behaving. You may have many classes in a game that is controlling audio, but it is a good idea to be able to trace all the decisions to one place. That way if there is a bug in your system, you can easily find and fix it. You can think of it as a team in a game company -- you have producers, programmers, artists, and musicians. They can all do many things and create great content, but you have to have somebody telling them how they are going to work together and what tasks they need to be doing. This is what a manager class does in programming.

IDE: An acronym for Integrated Development Environment. This is the program that you use to make programs and to write scripts. There are many popular ones including Visual Studio, Xcode, and MonoDevelop.

Singleton: A singleton is essentially a single instance of a class. If you create a singleton pattern in a class, it will make sure that only one of that class ever exists at a time. This is important when creating managers so that you don’t have two managers telling other parts of your code to behave in various ways. Having two managers at best leads to sound doubling, and at worst digital clipping and distortion, improper fading behavior, and a slew of other bugs.

Method / Function: A method, or function, is a piece of code that can be run to perform any number of tasks. In this video we are creating a method to play a sound at a specific point in space.

Access Modifier: Brennan types public when writing out his custom method. This is an access modifier. In other words depending on what you type here, other parts of your program may or may not be able to access this method. If your access modifier is public, other classes may access this method from a known reference. In this case, we are using a singleton, so there is only one reference to this class, and that is the Instance set up when making the class a singleton. If your access modifier is private, other classes will not be able to use this method, or even know of its existence.

Return Type: Brennan types the word void as well when declaring his method. This is the return type of the method. It determines what data can be returned when the method finishes executing. In this instance, the return type is void, meaning that no data will be returned once the method is finished. If a different return type is used, such as a float (number with a decimal point), then you can call a method to run a code block and retrieve a number all in one step. Otherwise, you would have to have that method save the number in another variable and store it somewhere. Using return types saves a lot of time and makes for cleaner code.

Arguments / Parameters: When writing out a method, you must define whether or not it takes arguments, and if it does, what they are and what their local names are. Any and all arguments are placed in the parenthesis after the method name, each one separated by a comma. These arguments are data types that must be passed into the method from another place in your program for the method to work. In this example, we need an audio clip and a game object so we know what sound to play and where to play it. The method is in the audio manager, but the audio manager has no idea what sound to play, it just knows that it needs some sound. So where do we get this sound? We get it from wherever we are calling this method from. When calling the method we will pass the correct data into it, and it will execute the code based on the data it was given.

Transform: This is essentially a set of coordinates in the game engine that tell us the position and rotation of an object on the x, y, and z axis.

If Statement: This is one of the most basic principals in programming. If a condition is true, a specified code block will run. If the condition is false, the code block within the if statement will be skipped altogether. The condition is in the parenthesis after the word if. If I am hungry, I will eat a sandwich. If I am not hungry, nothing will happen. Now, what if the condition for the if statement is false, and you don’t want to just skip everything and do nothing, you want something else to happen?

Else Statement: When an if statement is skipped because the condition was not met, an else statement can run a new code block in place of the if statement code block. If I am hungry I will eat a sandwich, or else I will put the sandwich in the fridge for later.

Fields / Variables: Fields are a way to store data in a class that can be used later. If I want to keep track of a specific number, game object, or piece of audio, I need to store it in a variable. If a field is public, it will show up on your script in the inspector, and you can drag in the correct data type. It this data will also be accessible from other classes if it is public. If the access modifier on the field if private, the only script who can access or modify the data is the class that you declared the field in.