Monster shadow


Manage your sound in Unity - Tutorial

Learn how to create a simple sound manager for your Unity game. Trigger sound from anywhere in your scene. Control your music, effects and master volume globally.

WS Project
thanks, why there is two method in OnClick event of buttons
Dr Meme
Thanks a lot for the tutorial If you wouldn't mind i need your help how would i go about playing certain music in certain while playing other music in the rest of the scenes?
Gregory Fenn
Just have more than one audio source in the scenes. The [DontDestroyOnLoad] attribute will be useful to make a song continue between scene loading
Foxer Hawkins
Thank you so much I've been looking for a audio tutorial that shows how to do the buttons and yours does thank you so much
You're welcome 😊
Raul Jeri Lara
What does your awake function of the sound manager do?
Raul Jeri Lara
@Tarodev ok. Thanks!
It's setting up the singleton pattern. It enforces only 1 instance of the sound manager will be active at any time. Also, as it's static you can access the sound manager all through your game like so: SoundManager.Instance
İlyas Köse
kamal S
Great tutorial , Speed , gravity, 3d behavior all things is in system but what about reading data from webcam & real time value changes & rendering applied? Do you have any idea? Any source for learning? Like this link Or even great if we can get tutorial from you👍


in this video i'll be showing you how to create a simple but powerful sound
manager it will allow you to easily trigger
audio from anywhere in your game it will give you one central place to
control your music effects and master volume
it's simple to create and easy to use let's get started
all right in our scene we've got a toggle music button a toggle effects
button a slider which currently doesn't do
anything and a spawn button which does exactly what
you expected just spawns a bunny um not very exciting
at the moment we've got no sounds so let's hook that up first up
we'll create a c-sharp script and let's call this a
sound manager open it up we'll start by removing these
two functions and creating an singleton out of our sound manager
if you're not sure what a singleton is it's a common design pattern in
programming which enforces that only a single
instance of this class will be running at any one time
so basically we're saying if there isn't an instance let's make this
new instance that has just been created the the instance
and will tell unity to never destroy it so even when we're changing scenes it
will always remain um active and if there's already a
instance then we'll just destroy this and it will
just be ignored next we'll need a way to actually play
the sound so this will take an audio clip as an
argument we're going to be needing some
references here so serialize field private audio source and let's have a
music source and an effect source so here we go we can just say effect
source play one shot and we'll put in our clip that's
actually all we need from our sound manager for now
let's pop back into unity create a new empty game object at the root of your
hierarchy and call this sound manager let's attach our new
c-sharp script now as a child of the sound manager create a new empty game
object and call it music source duplicate that
call it an effect source now let's add audio source components to
both of those and in our sound manager just slot in
the music source to the correct slot and the effects source
now now that we've got our sound manager set up we need a way to trigger the
sound so let's create one more c-sharp script
let's call this play sound on start
open him up delete the update method as we won't be using that
and create a serialized field private audio clip
and then in start we can reference our sound manager
instance play sound and just put in our clip all right back over to
unity on our bunny prefab i can now attach my
play sound on start and put in the audio effect that i would
like it to play and now if we just spawn our bunnies
we can hear a nice little spawn sound so let's hook up the volume
in a sound manager script we can create a new function and call
change master volume this will take a float as an argument
and as we're affecting the master volume here
we can simply grab the audio listener and we can say volume equals value
so this will be adjusted with a slider so back in unity we'll create
one more script and we'll call this a volume slider script
delete the update function as we won't be using it
let's create a reference to our slider
and in our start function we will hook on to the
on value changed event and we'll add a listener
and it will be take our float argument whoops sorry
value and we'll pass it to our sound manager instance
change master volume and we'll pass in our value
so back to unity to our slider and put our c-sharp script on there
and attach the reference and now we should just be able to
change the volume and there we go so that's how you
control the master volume uh lastly we'll be wanting to have these
toggle buttons um alternatively you could give your
users three sliders one for music effects and master volume
but for this i just wanted to show you how you can do it in
multiple ways so let's create a script here and we'll call it
toggle audio we'll start by creating two
booleans um one two toggle music and one to toggle our effects so we can
use the same script for both a public function called toggle
and we'll say if toggle effects then we'll do sound manager on instance
and now we need a function to toggle these in our sound manager
so back across to our sound manager all right so we'll need a
function called toggle effects and in here we're going to take our
effect source and we're going to say mute which is a
boolean equals the opposite of whatever
it currently is and we can do the same for the music
we'll just say music source is equal to the opposite of what music
source mute is currently so now we can say toggle effects
and we can also do the same for the music
so toggle music all right back to unity we can take our
toggle music button and first we're actually going to need
some music can't we because i haven't done that yet let's add some music
and i'll show you how that sounds it's very loud for me
there we go so the reason it wasn't taking effect straight away
is because in our volume slider we're not
setting the value until it's actually changed so before we continue let's just
do that volume slider on start as well let's say
sound manager instance change master volume
and let's say set it straight away to the value of the slider
so now when we press play it should be in the correct place
yep beautiful so let's toggle our music now so
on our music button let's add a click handler and we'll grab our toggle
audio script and we'll attach it and we'll say this one is just going to
toggle the music put it into our click handler toggle
audio and select the toggle button
let's give that a crack beautiful and we can do the same for our
effects just add a click handler add the script
but this time say toggle effects look that button up
and we can put there we go so now you know
how to toggle music toggle effects um one central place to control all of your
sound um and yeah i hope you learned something if
you did give it a like maybe subscribe and stay tuned