Hey.
Hello there, guys.
It's time to work on the audio in this game.
First, I want to organize our project just a bit.
I want to take these two scripts, drag it to the script folder.
And then just put these two in the UI folder.
Now let's create an empty object that I'm going to call audio.
Oh, dear.
Manager.
And we're going to create a new script.
C Sharp.
Oh, dear.
Manager.
And drag it here.
All right, now, let's go inside of the script.
We can close all but this and what and first, what we need to do is to make it accessible from any
place, just like we have with the game manager.
So I'm going to make it public.
Static Outdoor Manager.
Instance.
Yeah.
Then we need a wake function.
A wake in the wake, I'm going to say instance equals to this, and I think I can do.
Is what I can do.
I don't plan to write anything else in their wake.
When you start function.
All right.
So how this is going to work, you can create a type of variable that is called audio source.
Let's try this serialized field, private audio source.
Now, I'm going to call it a fix.
Very simple as a fix for sound effect.
I want to make it an array.
Let's make it an array and let's go to unit now.
There is a folder of audio in the project that I added just in the beginning, and there is an it's
a fixed folder and this is our sound effects.
So what we want to do is to take any of these sounds.
For example, let's take coin pickup, the one that we use the most.
First of all, when you drag any of the audio source uncheck play on a wake, because if it is on,
then whenever you start the game, the sound going to be played.
Let me unmute because now audio is muted.
So with the play on Awake, you can see that sound was played.
If you put more sounds into the game and you will keep playing awake.
And let me show you this.
I'm going to take jump jump and UI click and wasted drag it here.
They all have play on a wake function and if you go to play mode, it's going to sound horrible because
all of the sound is going to be played at once.
So what we need to do to fix that is to click play on a wake over here.
Then I want to give them parent.
And I'll name it as a suffix.
And then I'll drag this parent to the manager to make it a child of audio manager.
Good.
Now we have five sounds in the game.
There are some other sounds, but we're not going to touch them yet because now let's focus just on
creating an audio manager.
We have going to pick up a couple of jumps dest sound effect and UI click.
Now we're going to go to audio manager, click this, lock it, open this array and choose all of the
sounds and drag them here.
That's good.
Now that we have bunch of elements in the array, we can use them, we can call them and tell them,
Just play this what we're going to do, Let's go back.
I'll delete these two functions for now, and I'll create one public void.
Play as a fix.
I need an index for the sound.
So and I and then just inside, I'm going to do this.
If I is less than as a fixed length and we're using this one just to check if there is such a number
in the array, then we're going to play the sound.
It's a fix.
I don't play very simple stuff.
Let's go and try this.
Let's go to the coin and trigger enter of the coin.
We're going to call audio manager dot instance.
Dot play is a fix and we need to check what was the number because I don't remember.
Coin pickup is on the zero.
So let's do this zero.
Let's go and see.
How does this work?
Okay.
That is very good.
Now, let's go back and create another function that we might use just in some cases.
But still, it's better to have this function public void stop as a fix int i.
And inside we're going to do.
Suffix when I stop.
Now regarding background music, basically the same.
The only difference is when we play background music, we need to make sure we switch off all other
background music there is to avoid the chaos from different audio sources.
So let's do sterilize field here.
Private audio source for BGM.
Then we're going to create a function public void play BGM.
We're going to pass into a because we need a number.
Then we're going to do for loop.
They're going to cycle through all BGM sounds.
The GM dot links.
GM I dot stop and then we're going to play some we want.
Basically we did same functionality when we did switch UI if you remember I dot play and then just in
case you need to stop GM or we have some issue here because for a loop using AI and we as an AI, there
is a conflict because of the same name.
So we better change this to something like index, for example, index.
Or we could use just a tee.
Well, let's be consistent and type indexes here as well.
Okay.
Now, let's do public void.
Stop.
We don't need to pass index here.
We're just going to switch off all of the BGM music.
Okay, good enough.
Now let's go and prepare background music.
Going to go to audio folder, find BGM.
Drag them to the hierarchy, then let's create a parent for them that's going to be named as PG.
Let's unlock the inspector.
Big jam.
Then we're going to make a child of audio manager.
And then we need to draw songs to the mirror.
Just like this.
And check make sure we uncheck play on awake here.
And don't worry about the volume.
We're going to fix volume later.
For now, let's just do this.
Let's make sure it's working so we can go to audio manager.
Again and sorry.
And in this third function.
We can do a start.
Maybe GM with the random index.
So random range.
And if yours, just like mine, shows this weird error, what you can do is go above and just delete
this using system and delete using system reflection.
We don't need them.
We only need collection and collection generic.
Now it should be fine.
So not range from zero to BGM dot lengths.
So it's going to play a random background music.
Cool.
Now let's go back to Unity and check this out.
All right, great.
Now let's go and do other sounds like jump and death.
First, we need to find player.
Find his jump function.
Let's see.
Where is it?
So in the jump here, we're going to do our job manager.
That instance does not play as a fix.
What was the number?
First and second so we can do random range between two sounds, random range.
Again.
My range is not here.
Let's fix it.
Let's go above and delete this system.
This component model also to know why it's there.
Just sometimes Intel sends trolls different random stuff to your cold.
Happens sometimes from 0 to 1, a weight from 1 to 2.
Right.
And I want to copy this line of code and put this to double jump as well.
Now whenever we di let's go find di function.
We're going to play this sound.
Here.
And your manager always have trouble to type in this.
Oh dear.
Manager.
Instance Dot place a fix.
And the number is.
Three.
Number three, I also think we need some sound for knockback, but let's take care of that later.
We also need UI click whenever you click any UI buttons.
So let's go to your main and add UI sound here.
Add your manager dot instance.
Place a fix, and I guess that was the last one.
Number four.
Yes, We also can use this sound whenever you try to purchase something.
So let's go and find your shop.
And assign the sound to purchase color over here.
Now, let's go and see if all of that works.
I'm going to bring up volume just a bit and test it.
Music is there.
Shop is there.
I can hear the clicks.
We also can find some sounds for successful purchase or unsuccessful purchase.
I'll see if I have something I'll attach to the lecture.
But in any case, I will show you how to find your sound effects and music later in the course.
Just there will be a special video about that.
How to search for sounds.
This works this.
We need to make it work later.
It's going to be in the next section.
Have to start.
Jump is working.
Big cap is working.
And let's try this sound.
Cool.
There is something else to do at the moment.
If you play background, music is going to play once and then it's going to stop and we need to make
the functionality to play new song again.
After the background music step, you need to play next song, write this one.
We need to do.
So.
What we can do is just to change the functionality by a bit.
Let's create a function that going to say.
Public void play random BGM.
We need a private integer for BGM index.
So private int BGM index.
Then we can assign random value to this index in the play.
Random BGM BGM index equals to.
Let's just cut it out from here.
Paste it in here and then take this line of code.
Go to play random BGM.
Paste it in with the BGM index.
So now whenever you want to change the song, you can call this function.
It's going to stop all of the music that plays now and play new random song.
No matter how many songs you have, is going to choose random one from the list.
This is good.
Now the next one is to make an update function instead of start in the update function.
We're going to check if music is playing or not, so we're going to do it.
If no BGM with the BGM index lane is playing.
So if not playing, then we can play new random song.
Play random BGM.
In this way, we're going to do check.
And by default, our BGM index is zero.
So when our game starts and first update runs, it's going to see if BG zero is not playing, then we're
going to say, Play random BGM and this play Random BGM.
Going to take a new index from zero to BGM lengths.
It's going to play the sound.
It's going to play the music with new BGM.
And as you know, inside of that, we're going to switch off all of the music that we had, if any,
played play new sound.
And by that moment, BGM Index is going to be new, for example, is going to be number one.
BGM index equals to one.
Then update going to check if BGM plane is going to do nothing.
If it's not playing, it's going to play new song.
Very simple.
Very cool.
All right.
That's it for this video.
And I'll see you in the next one.