Loop Audio between Scenes and Add Sounds Effects - Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
our games looking a little bland how do we add sound effects and audio to it that seamlessly loops through different scenes well it's actually pretty simple so first we want to start off with actually getting some music into our game so under our assets we can make a new folder so right click create folder and then we will create a folder called music enter and then you can download music from any website that you want so right now I'm on incompetent they have royalty-free music which basically means you don't have to pay for the music you go to the one you want in this case I'm gonna use pixel and you download and then choose free license and you have to give them attribution if you don't want to give them attribution then you have to actually pay for the music so I'm going to be using pixel and for the music and for the sound effect I'm gonna be using the sound effect when the player dies I'm gonna play the sound effect and I'm using one made by Dan Lucas and then he says you can use my sounds freely it would be great if you can credit me so they also have the license right here alright let's head to it so in your file explorer you'll have your music so you can just drag and drop the music into your assets folder and it will import it and then when you click on the audio you can see there's some extra settings in there but we won't be using those for now so to add music to our game we can just right click on the hierarchy we can create empty and then I'm gonna press f2 and I'm gonna rename this to music player so we have to have a game object and then we have to add an audio source to our game object so on the right under the inspector we can add a component and I'm gonna add in an audio source and then you can just drag in that pixel and audio clip and once the level starts playing this will start playing because we have the play on awake check marked and then to loop it all you have to do is press this little checkmark here and it will automatically loop it you can also play with the settings here the priority will determine how much priority it has over different audio sources so the one with more priority will be heard a little louder than those with lower priority and then we have our volume the pitch and you can just play around if you need to so once we click play our audio started playing which is awesome currently it's a little out of mine so I'm just gonna put the volume down to maybe 0.6 and now the issue with this is that this is going to be destroyed when we load in a new scene or when we collide with an enemy we currently just reload the scene and every time we do that the music will restart and that is not what we want so to counteract that we can go into our assets under our scripts and we can make a new script so right click create c-sharp script and we will call this script music manager probably not the best name but it'll do for now so then this double-click that and now we have our music manager script I'm gonna erase these comments here we do not need a void update function and I do not need these two using statements so how's this gonna work out so unity has a do not destroy onload function and basically it does not destroy the target object when loading a new scene so we can apply this do not destroy don't destroy onload to our script so in our script we're actually gonna put this to awake which runs before start because we want this to be done at the beginning before the level actually loads so we can say don't destroy onload and then we have to pass in what we don't want to destroy and so we're going to pass in this and this basically references our music manager script because it's referring to this script and that's great enough but what happens when the level initializes again so you'll have this script that did not destroy and then the level will initialize and create another game object called music player with the music so there'll be two musics running at the same time and we don't want that so there's a way to make a music manager keep a reference to itself and then if it detects a new reference then it just destroyed that reference and just keeps the same reference that it's always had so to keep it reference to our music manager we're gonna want to do private static music manager and we're gonna call this music manager instance but what is static well static makes an object non-intentional meaning we don't have to instantiate this object with the new keyword as you may have noticed with our vector3 we have to do vector3 equals new vector3 but with static variables we don't have to do that and when you declare a variable a static that means we only have one copy of this variable at a time and you can share this variable among all of the objects in this class have you made it public for example then in player controller I can easily do a music manager which is the name of the script dot and then music manager instance and then I'd have a reference to the instance of that music manager and I can call any of the scripts in that music manager however this is generally discouraged as it encourages disorganized code and can lead to difficult Mis and debugging but in this case we're just gonna make it private and we're just gonna use it to make sure the music manager is not instantiating twice so in our awake function we're gonna say if music manager instance equals no so if we don't have a reference then assign a reference so mutant music manager instance equals this else we're gonna destroy game object so this is going to destroy the game object this script is attached to meaning it will destroy the new object that's created once we reload the scene and just save that and then under our music player in our hierarchy we can click it and then we can add a component and we can add in a music manager and that will actually keep this game object through your different scenes and you can also make another script if you have maybe a couple songs that you want to switch at certain times but that's of it now let's add a sound effect whenever our player hits a enemy so you saw that we can add an audio source to our game objects but what happened if we have a lot of audio and we don't want to essentially add a bunch of audio sources and then have to index each one by an array well there's actually something called a resources folder so under assets we can right click create folder and we can have we have to call it resources just like that make sure this is correct or our unity will not find it so in our resources we can load objects and assets during game time so this is useful if you have a lot of game objects but you don't know exactly which ones you want to instantiate at the beginning so you put them in the resources and then you load them in as you need so in our music we're actually gonna drag our audio to that folder so you can click the the FX and then drag it to your resources folder so I'm just gonna drag it to assets because I don't see it right now and then I'm gonna drag it to resources and then we can press f2 and we're gonna rename it death sound alright so now let's load in this def sound so if we go to our player controller which is under assets scripts player controller double click that then you can see that under our on collision 2d enter 2d we have our load scene so every time we hit an enemy the scene reloads so here's we want when we want to play the sound and we can actually keep a reference to the sound so let's do a private audio clip sound def sound I'm gonna call it def sound actually sounds better and then I'm actually do it's gonna do this in start I'm gonna say def sound equals resources dot load and then all you have to do is provide the name of your sound so in here you pass in a file path and it's already pointing to the resources folder so since it's a pointing here we just need to pass in def sound so we just loaded def sound currently we are getting an error it says cannot implicitly convert type unity object to unity engine audio clip clip meaning that currently it's not recognizing that it's an audio clip and this is an easy fix all you have to do is cast it so at the beginning we can say audio clip and this will cast the resource that we load to an audio clip so it can match the object up here we actually did have a start function already so I'm just gonna erase that real quick and then just paste it in the start function that we already have because you can't have two functions with the same name and then all we have to do is play the audio clip when our player hits the enemy and then we actually have to have an audio source on the player so if you go back to the unity editor under player we have to have an audio source because we're gonna pass in our audio clip into this audio source right there we're gonna pass it in through our code and then we can play it and let's uncheck play on awake because we don't want to play this on awake we want to play it when we say to play it and then here on our Uncle Asian enter to D before we load the scene again we're gonna actually assign our def sound clip to our audio source so we just go audio source clip equals def sound and then you can say audio source dot play now there is a problem with this we're pressing we're saying play the audio source but we're also saying load the scene immediately after so unity will not be able to play the audio source because it loads the scene too quickly and you won't actually hear anything so there is a way to actually wait until this is done also there's better ways of doing this you can probably put this in your music manager script and then just you can actually get a reference to that music manager script and pass in the clip itself and then in the music manager it can play it for you but in this case I want to show you how to use a current een but I'm pretty sure I pronounced that wrong I've never been able to say it right but a curtain is basically a function that runs to completion before returning and you can actually specify when you want it to continue running so so let's see an example here first we have to start the curtain which it starts the function and then in the function itself you can do some math you can do some assignment property so you can say at the top boolean equals true and then you can say yield return new waitforseconds and this is great if you want to wait for a certain time so you can say wait for three seconds before doing this so if you're jumping you can jump and then you can call this curtain and then at the top you can say jumping equals true so then if you're jumping and you don't want to jump again you don't want the player to jump again for three seconds you can say yield return you wait four seconds three seconds and then you can say jumping equals true or false basically allows you to set variables one once a certain time limit has been reached and it doesn't also only have to be a time limit you can set it for waiting for specific conditions so in this case we can set it for until our audio source has stopped playing there's a bunch of other examples here but I will show you it now so the syntax is ie numerator and then you have your function name I'm just gonna call it play sound and then we have to have a yield return new and here are the several things we can put wait for and a frame wait for fixed update wait for seconds wait for seconds real time but in this case we want to say wait until so we want to say wait until something is done so in the way until function we can say wait until the audio source is done playing so the syntax for this is parentheses and then an arrow and then we can do our conditions so we can say audio source da is playing equals to false so is the audio source playing if it is playing then we still can't return and if it isn't playing so if it equals false then we can continue and so we can actually copy this load scene and we can paste it under our yield return so this will actually stop this function from continuing on until this condition has been met so until the audio source has stopped playing it will not execute this but once it has stopped playing it will execute this and so up here on our on collision to the Enter we can actually copy this audio source clip and then we can just paste it down here under play sound right before the yield return so it will start to play the audio and then here we can just call start curtain once again I probably saying that wrong and you can just pass in the function itself play sound so we pass in the function and then this will start it will assign the clip it will play the clip and then it will not continue on in this function until this condition has been met once it has been met then we load in the new scene so we can minimize now and when we click play then it will start over it's a little iffy now in most cases most games play an animation and then until the animation is done then it reloads the scene but this is just an example for now you can also use a shorter sound so that it doesn't take us long but yeah that's just a bit if you enjoyed this video please like and subscribe I hope it helped and we're almost nearing the end of this beginner tutorial series I have about one or two episodes left to go and after that I'll be posting some interesting stuff so if you're interested please and I'll see you next time [Music] you
Info
Channel: samyam
Views: 15,496
Rating: undefined out of 5
Keywords: samyam, unity3d, unity, tutorial, game dev, game development, unity beginner tutorials, unity sound effects, unity sound system, unity add music, unity how to add music, unity loop music, how to loop sounds in unity, loop sound, loop audio, audio singleton
Id: _5ekbSrNcPU
Channel Id: undefined
Length: 15min 22sec (922 seconds)
Published: Tue Jun 09 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.