Right.
Let's add some spicy music to your game because having a background music looping around is awesome
and that's what we're going to add.
So let's go to the asset store and search for absolutely, absolutely free music because there is a
great asset, absolutely free music, loads of positive reviews.
And yeah, we can simply download it.
So let's download that.
It's a 160 megabyte file, so it could take a while, but we're not going to import all of it.
So just go ahead and download it.
It will take a little while.
So I'm going to pause until the download is done.
All right, my download is ready and I don't want to import all of the audio as this will make our game
huge.
The only one that I want to use is the seven.
So with Love from Vertex Studio seven.
So let's import that.
It fits a little bit into the genre of our game, which is an endless game and it's going to have some
cool sounds.
So let's go to our asset folder real quick and create a new folder which I'm going to call third party.
And then here I want to drag the game music, the hand-painted stone texture, then the stylized crystal
and the Super Saiyan character pack free because those are all three, those are third party assets.
And it's good to have our own structure here, which is clean.
All right.
So now go ahead and create a new object here.
Create empty.
And I'm going to call that one background
loop.
And also I'm going to create a new script, which I'm going to call background loop as well.
Background loop.
And I'm going to drag that background loop into my background well, that script into my background
loop game object.
And on top of that, I'm going to reset it.
Now the background loop should have an audio source.
So audio source is what I'm going to add.
And now I need an audio clip and we have with love from Vertex Studio, so let's use that then play
on Awake should be checked as well as Loop because I want this to loop through now the volume will be
a bit loud, so let's reduce that to 0.3 maybe.
And well, let's check it out.
And as you hear, the music is running in the background and maybe it's even a bit too loud.
So I'm going to reduce the sound a little bit now during the game.
So now let's check it out.
And yeah, look, it looks good.
Looks great.
And now let's drop.
And as you can see, it simply restarts the scene and the loop is restarted as well, or the music is
restarted as well.
And what we want to do is we want to keep that game object alive so that the music doesn't restart after
we have died.
So it should go on and there is a method that does that.
So let's go ahead into our script.
So let's go into our background loop.
Let's get rid of the start and update method and we're just going to use the awake method here.
Void awake.
And then here we call the method.
Don't destroy a load.
What this does is makes the object target not be destroyed automatically when loading, loading a new
scene.
That's what we want to have.
And which game object shouldn't be destroyed?
Well, I'm just going to say game object, which means the game object that contains this script.
So that means that our background loop game object will survive a move from one scene to the other.
Now let's check that out.
Let's run the game and see if that works.
As you can see now we have this little unity sign here.
And let's run the game.
Let's collect some points and fall.
And you will see that now we have don't destroy a load.
And we have two of those.
So we have the background loop and another background loop, and that's not what we wanted.
We only want to have one of them.
So how do we solve that?
Well, there is a way to do that.
If we go to our code, we will need a public static variable.
So we will need to create a static background loop variable, which is an instance.
So I'm going to call it an instance because I want to only have one instance of that game object.
So let's check it out in our Awake method.
I'm going to check if instance is null because if it is null, then the instance is going to be this,
which means this game object.
So this particular game object of our current scene or current game is going to be the instance that
we want to work with because we didn't have an instance before that, so we didn't have a background
loop before that.
But if we did have one, so else if the instance is not this instance.
Destroy it.
So destroy game object.
So that means if the instance that we had before that so if it wasn't null, if it's not the one that
we are currently working or that we currently are our self, then destroy ourselves.
So that means let the other instance or the other instance of background loop, let it survive.
All right.
So that's what this code does.
So what it will allow us is to only have one background loop all the time and not create a new one after
having changed the scene.
So now let's check it out again.
Of course, I have to reduce the volume because now otherwise it will be too loud.
So let's die.
And as you can see, our music kept on playing.
All right.
So that's how we can create a loop for our background music.
All right.
So see you in the next video where we are going to create our world procedurally.