1 00:00:04,080 --> 00:00:04,400 ‫Right. 2 00:00:04,410 --> 00:00:10,980 ‫Let's add some spicy music to your game because having a background music looping around is awesome 3 00:00:10,980 --> 00:00:12,090 ‫and that's what we're going to add. 4 00:00:12,090 --> 00:00:24,990 ‫So let's go to the asset store and search for absolutely, absolutely free music because there is a 5 00:00:24,990 --> 00:00:30,360 ‫great asset, absolutely free music, loads of positive reviews. 6 00:00:30,450 --> 00:00:33,810 ‫And yeah, we can simply download it. 7 00:00:33,810 --> 00:00:35,010 ‫So let's download that. 8 00:00:35,010 --> 00:00:41,190 ‫It's a 160 megabyte file, so it could take a while, but we're not going to import all of it. 9 00:00:41,190 --> 00:00:42,990 ‫So just go ahead and download it. 10 00:00:42,990 --> 00:00:44,100 ‫It will take a little while. 11 00:00:44,100 --> 00:00:46,440 ‫So I'm going to pause until the download is done. 12 00:00:46,620 --> 00:00:51,870 ‫All right, my download is ready and I don't want to import all of the audio as this will make our game 13 00:00:51,870 --> 00:00:52,500 ‫huge. 14 00:00:52,500 --> 00:00:55,290 ‫The only one that I want to use is the seven. 15 00:00:55,290 --> 00:00:58,440 ‫So with Love from Vertex Studio seven. 16 00:00:58,440 --> 00:00:59,610 ‫So let's import that. 17 00:00:59,610 --> 00:01:04,440 ‫It fits a little bit into the genre of our game, which is an endless game and it's going to have some 18 00:01:04,440 --> 00:01:05,250 ‫cool sounds. 19 00:01:05,250 --> 00:01:12,540 ‫So let's go to our asset folder real quick and create a new folder which I'm going to call third party. 20 00:01:13,050 --> 00:01:22,290 ‫And then here I want to drag the game music, the hand-painted stone texture, then the stylized crystal 21 00:01:23,280 --> 00:01:31,080 ‫and the Super Saiyan character pack free because those are all three, those are third party assets. 22 00:01:31,080 --> 00:01:34,260 ‫And it's good to have our own structure here, which is clean. 23 00:01:35,010 --> 00:01:35,460 ‫All right. 24 00:01:35,460 --> 00:01:41,070 ‫So now go ahead and create a new object here. 25 00:01:41,070 --> 00:01:42,300 ‫Create empty. 26 00:01:42,450 --> 00:01:44,940 ‫And I'm going to call that one background 27 00:01:47,490 --> 00:01:48,120 ‫loop. 28 00:01:49,290 --> 00:01:54,060 ‫And also I'm going to create a new script, which I'm going to call background loop as well. 29 00:01:54,060 --> 00:01:55,950 ‫Background loop. 30 00:01:56,670 --> 00:02:01,470 ‫And I'm going to drag that background loop into my background well, that script into my background 31 00:02:01,470 --> 00:02:02,670 ‫loop game object. 32 00:02:02,850 --> 00:02:05,640 ‫And on top of that, I'm going to reset it. 33 00:02:06,420 --> 00:02:11,160 ‫Now the background loop should have an audio source. 34 00:02:11,160 --> 00:02:14,580 ‫So audio source is what I'm going to add. 35 00:02:14,580 --> 00:02:21,420 ‫And now I need an audio clip and we have with love from Vertex Studio, so let's use that then play 36 00:02:21,420 --> 00:02:28,050 ‫on Awake should be checked as well as Loop because I want this to loop through now the volume will be 37 00:02:28,050 --> 00:02:30,990 ‫a bit loud, so let's reduce that to 0.3 maybe. 38 00:02:31,320 --> 00:02:34,350 ‫And well, let's check it out. 39 00:02:37,290 --> 00:02:42,090 ‫And as you hear, the music is running in the background and maybe it's even a bit too loud. 40 00:02:42,090 --> 00:02:45,120 ‫So I'm going to reduce the sound a little bit now during the game. 41 00:02:45,120 --> 00:02:46,680 ‫So now let's check it out. 42 00:02:47,340 --> 00:02:49,080 ‫And yeah, look, it looks good. 43 00:02:49,080 --> 00:02:49,680 ‫Looks great. 44 00:02:49,680 --> 00:02:51,120 ‫And now let's drop. 45 00:02:51,330 --> 00:02:57,330 ‫And as you can see, it simply restarts the scene and the loop is restarted as well, or the music is 46 00:02:57,330 --> 00:02:58,440 ‫restarted as well. 47 00:02:58,440 --> 00:03:04,620 ‫And what we want to do is we want to keep that game object alive so that the music doesn't restart after 48 00:03:04,620 --> 00:03:05,850 ‫we have died. 49 00:03:05,850 --> 00:03:09,290 ‫So it should go on and there is a method that does that. 50 00:03:09,300 --> 00:03:11,640 ‫So let's go ahead into our script. 51 00:03:11,820 --> 00:03:13,980 ‫So let's go into our background loop. 52 00:03:15,460 --> 00:03:22,330 ‫Let's get rid of the start and update method and we're just going to use the awake method here. 53 00:03:28,170 --> 00:03:29,550 ‫Void awake. 54 00:03:30,060 --> 00:03:31,920 ‫And then here we call the method. 55 00:03:31,920 --> 00:03:33,270 ‫Don't destroy a load. 56 00:03:33,450 --> 00:03:39,600 ‫What this does is makes the object target not be destroyed automatically when loading, loading a new 57 00:03:39,600 --> 00:03:40,120 ‫scene. 58 00:03:40,240 --> 00:03:41,580 ‫That's what we want to have. 59 00:03:41,580 --> 00:03:44,880 ‫And which game object shouldn't be destroyed? 60 00:03:44,910 --> 00:03:50,910 ‫Well, I'm just going to say game object, which means the game object that contains this script. 61 00:03:51,210 --> 00:03:57,630 ‫So that means that our background loop game object will survive a move from one scene to the other. 62 00:03:57,870 --> 00:03:59,370 ‫Now let's check that out. 63 00:03:59,370 --> 00:04:01,560 ‫Let's run the game and see if that works. 64 00:04:02,460 --> 00:04:06,000 ‫As you can see now we have this little unity sign here. 65 00:04:08,010 --> 00:04:10,740 ‫And let's run the game. 66 00:04:11,460 --> 00:04:13,410 ‫Let's collect some points and fall. 67 00:04:13,980 --> 00:04:17,670 ‫And you will see that now we have don't destroy a load. 68 00:04:18,300 --> 00:04:19,350 ‫And we have two of those. 69 00:04:19,350 --> 00:04:24,150 ‫So we have the background loop and another background loop, and that's not what we wanted. 70 00:04:24,150 --> 00:04:26,880 ‫We only want to have one of them. 71 00:04:26,880 --> 00:04:28,620 ‫So how do we solve that? 72 00:04:28,860 --> 00:04:30,530 ‫Well, there is a way to do that. 73 00:04:30,540 --> 00:04:36,960 ‫If we go to our code, we will need a public static variable. 74 00:04:36,960 --> 00:04:42,390 ‫So we will need to create a static background loop variable, which is an instance. 75 00:04:42,390 --> 00:04:47,850 ‫So I'm going to call it an instance because I want to only have one instance of that game object. 76 00:04:47,910 --> 00:04:51,230 ‫So let's check it out in our Awake method. 77 00:04:51,240 --> 00:05:00,840 ‫I'm going to check if instance is null because if it is null, then the instance is going to be this, 78 00:05:00,840 --> 00:05:02,550 ‫which means this game object. 79 00:05:02,550 --> 00:05:09,960 ‫So this particular game object of our current scene or current game is going to be the instance that 80 00:05:09,960 --> 00:05:14,460 ‫we want to work with because we didn't have an instance before that, so we didn't have a background 81 00:05:14,460 --> 00:05:15,390 ‫loop before that. 82 00:05:15,390 --> 00:05:26,280 ‫But if we did have one, so else if the instance is not this instance. 83 00:05:27,890 --> 00:05:28,660 ‫Destroy it. 84 00:05:29,690 --> 00:05:31,660 ‫So destroy game object. 85 00:05:31,670 --> 00:05:39,350 ‫So that means if the instance that we had before that so if it wasn't null, if it's not the one that 86 00:05:39,350 --> 00:05:45,020 ‫we are currently working or that we currently are our self, then destroy ourselves. 87 00:05:45,230 --> 00:05:51,190 ‫So that means let the other instance or the other instance of background loop, let it survive. 88 00:05:52,190 --> 00:05:52,760 ‫All right. 89 00:05:52,910 --> 00:05:54,410 ‫So that's what this code does. 90 00:05:54,410 --> 00:06:01,340 ‫So what it will allow us is to only have one background loop all the time and not create a new one after 91 00:06:01,340 --> 00:06:03,050 ‫having changed the scene. 92 00:06:03,530 --> 00:06:05,270 ‫So now let's check it out again. 93 00:06:05,300 --> 00:06:09,950 ‫Of course, I have to reduce the volume because now otherwise it will be too loud. 94 00:06:09,950 --> 00:06:11,420 ‫So let's die. 95 00:06:13,160 --> 00:06:15,980 ‫And as you can see, our music kept on playing. 96 00:06:16,250 --> 00:06:16,700 ‫All right. 97 00:06:16,700 --> 00:06:21,140 ‫So that's how we can create a loop for our background music. 98 00:06:21,290 --> 00:06:21,680 ‫All right. 99 00:06:21,680 --> 00:06:26,300 ‫So see you in the next video where we are going to create our world procedurally.