In this lecture we'll look at the implementation of sound effects in our game. One common approach to doing audio in games is to attach an audio source to the game object that sort of conceptually makes that sound. There's a problem with that approach in this particular game and in general. So, let's think about the explosion for example. The explosion would be the appropriate place to add an audio source to. And once the explosions animation ends, the explosion destroys itself. The problem is that the audio source attached to the explosion game object is also destroyed as it should be. But if the audio clip for the explosion hasn't finished playing yet then it sounds cut off, because it's been cut off. And I've also run into this problem within asteroids game, where an asteroid gets hit by a bullet, they are both getting destroyed something should make a noise but neither one of them can because it's been destroyed. A good general solution to that problem is to have a single audio source that will play all the audio in our game, and then have an audio manager that uses that game audio source to play the audio. And that's the approach that I've used in this game, you can see the scripts that I have are audio clip name, audio manager and game audio source. And I also have a game audio source object added to the game. And the only thing it has at this point is a transform and the game audio source script attached as the component. So, let's go look at the code to see how all this audios stuff works in this game. Remember that the game audio source script is attached to the game AudioSource gameObject. In Awake we make sure we only have one of these. So we don't want multiple game AudioSource, gameObject in the game. And this could happen if we left the main menu and then came back to the main menu. Because when we came back to the main menu scene, another game audio source object would get added to the scene. So here's where we check that. And what keeps track of whether we've got one or not is the audio manager script will tell us whether or not it's been initialized. If it hasn't been initialized then this is the first game audio source object we've added to the game. So we do a number of things. We add an AudioSource component to the game object. We call the initialize method with that new audio source and we call this method built into unity called DontDestroyOnLoad. And we pass in the game object, we want to persist across multiple scenes. So if you call this method you actually get that gameObject throughout the scenes in your game until you decide to destroy it. And that's a good thing because we want the audio in every scene in our game, not just in one. If in fact, we already have a game audioSource object, we just destroy the one we're creating here as we enter the main menu because it's a duplicate. AudioClipName is just an enumeration of all the audio clip names that we're going to play in the game. And then the AudioManager ties all this together. So it has a field to tell whether or not it's been initialized, it has an audioSource field so it can save that audio source and then it has a dictionary. So these dictionaries are really useful, we've seen them all over the place in this game. And they are generally really useful in game development. This Dictionary is keyed by the AudioClipName and the value is the clip for that AudioClipName. Here's that property that tells whether of not we've been initialized. Here's the initialized method. So we set the flag to true, so that we know we've now been initialized. We set our audioSource fields to the parameter, the argument that was passed in from the other side for our audio source. And then we add each clip to our dictionary. So here's the key for this particular audio clip. And then we do this runtime loading of a resource that we've seen regularly as well. So we load all the audio clips. And what's nice about using this dictionary is if somebody decides that BurgerDamage isn't the right name for the actual file, that we're going to play. I use WAV files but Unity support a variety of different audio sources. Then all you have to change is this name here and nothing else in the game has to be changed and that's nice. So it protects the rest of the game from file name changes, when somebody decides to change the name of a WAV file. And now, at this point when we're initialized, we are holding the audio source in the field and we have our dictionary loaded up with all the clips that we might play as part of the game. And the last piece of the audio-manager is this play method that other classes in our game can call to play a particular audio clip. And the way that works is, it looks up the audio clip in the dictionary using the audioClips[name] that was passed in, and then it calls PlayOneShot on the audioSource object. Now, you may have seen audio played with the play method, and that's great, it's awesome. But PlayOneShot is sort of a fire and forget way to play audio clips. So having a single audioSource play all our clips we certainly want them to be able to overlap and so on, so we can't sort of tie up the audioSource with a single clip to play to completion using the play method on that audioSource. Instead we call this PlayOneShot and it starts up that audio clip, and then it's ready to play some more as necessary. So that's why we want to use PlayOneShot with our single audio source in the game. So we can see who calls this play method, the Burger class calls it three times, Explosion calls it once and so on. There are 16 matches including the definition of the method. So this method is called 15 times by multiple objects in the game to play our audio. And that's all there is to implementing the audio for the feed the Teddy's game. To recap, in this lecture we saw how to implement a simple sound system using a game audioSource and an audioManager. We saw that let us make it so that none of the game objects had to have an audio source attached, which simplifies those game objects and our editing process in the Unity editor. And we also saw that this approach works where having game objects play their own sound effects doesn't work when in fact, they would clip the sound effect by being destroyed before the sound effect was done being played.