That feeling when you follow the tutorial, set everything up, tear your hair out for 3 hours wondering why the sound won't play... and then change the pitch from 0 to 1.
If anyone is curious on how to stop playing a looping sound (maybe you want to switch bg music when you enter a new area) just use s.source.Stop (); You can just add a method to the Audio Manager like this public void StopPlaying (string sound) { Sound s = Array.Find(sounds, item => item.name == sound); if (s == null) { Debug.LogWarning("Sound: " + name + " not found!"); return; } s.source.volume = s.volume * (1f + UnityEngine.Random.Range(-s.volumeVariance / 2f, s.volumeVariance / 2f)); s.source.pitch = s.pitch * (1f + UnityEngine.Random.Range(-s.pitchVariance / 2f, s.pitchVariance / 2f)); s.source.Stop (); } just call it like this (AudioManagerReferenceGoeshere).StopPlaying("sound string name");
Hey Nightcore Motion, I was just like you using s.source.Stop(), but it keeps giving me a NullReferenceException that the source equals to null, any idea on how to fix that?
You should actually write it at the place where you want to stop bgm 1 and play bgm 2. if (blah blah){ FindObjectOfType ().StopPlaying ("GameplayMusic"); FindObjectOfType().Play("MenuMusic"); }
This video is 4 years old, but it still legit!! I was struggling to find a way to manage game audio files until watching this. Many thanks to Brackeys!!
OK Guys, for some of you that will likely be getting a Null:Exception Error you might need to check that you aren't trying to Play() the sound in an AWAKE function on another object. I had this problem and for about a month I've been pushing it to the side and figured I'd get around to fixing it later when it became a little more important. SO the problem I had was I used the same setup as in the tutorial, with another class I called Jukebox. This was to handle the current soundtrack playing and to allow players to cycle between other soundtracks in the game, like a Jukebox :D Long story short, I was calling the following lines of code: audioManager = FindObjectOfType(); audioManager.Play("music_metal_1"); within the Jukebox.Awake() function. This is a NO NO. The SOURCES aren't populated until later which means when I was trying to invoke them they hadn't actually been setup ;) To get around this I simply called the same code in the Jukebox.Start() function which worked absolutely fine. I hope this helps one other poor soul out there then I know my suffering was not in vain. This post isn't actively monitored so my query went almost 2 months without a single response. I hope this helps someone. Never Give Up
@@Noobyoulus So basically, only use Awake() to initialize the class that method is in, and use Start() to get references from other classes, making sure the other classes have already been initialized by the Awake method.
Move these two lines of code: s.source.volume = s.volume; s.source.pitch = s.pitch; from Awake to the Play function. That way you can adjust those values in real time while the game is running.
For anyone that's struggling with ArgumentNullExceptions when you switch scenes, it's because you're referencing the wrong AudioManager. For example, if you switch scenes, the AudioManager in the scene gets destroyed by the singleton code, but you call Find() on it instead of the singleton instance of the AudioManager from a previous scene. In other words, you shouldn't use Find() but rather AudioManager.instance when referencing the AudioManager. In fact, if your game always starts at the main menu of your game then you only need one AudioManager instance in the main menu hierarchy, but I'd still recommend keeping AudioManagers in every scene.
If you are new to coding and you arnt able to take classes I would suggest following along and paying attention with alot of beginner coding videos, it really does help, this channel its self is probably one of the best places to start.
Thank you a lot for this tutorial. I spent multiple days figuring out a way to keep the music nice and clean throughout scenes and after finding a somewhat working method, your tutorial is like a gift from the heavens (the internet heavens). Love your tutorials, love your good attitude, can't wait for the next one!
I have watched your videos for ages , learning a shit ton from them. I just realized i never thanked you for the videos. So.. THANK YOU! You are really motivating me through my struggles :D
@@fv4202x My interest in game development has definitely decreased, but my skills and craft have definitely increased. I'm still planning on releasing games one day.
It`s amazing that there are still people like you who keep helping others without wanting something for exchange. Love your vids. I wish you luck and don`t stop making this awesome tutorials.
Finally, I've been looking at a bunch of overly complicated sound managers that I can't seem to get to work, and then you come along and make it so much simpler and actually work.
me too but.... "MissingReferenceException: The object of type 'AudioSource' has been destroyed but you are still trying to access it" and no I didn't alter the code
Very nice solution, if I may add one thing then instead of finding the gameobject reference every time you want to play a sound, you could write a static function in the audiomanager: public static void PlaySoundStatic(string name) { if (instance == null) { Debug.Log("No audio manager "); return; } instance.PlaySound(name); } Then we use the current active audiomanager to play the sound. Then you would write "AudioManager.PlaySoundStatic(string mySoundName);" to play a sound from anywhere.
MAKE SURE you give the pitch a value that is NOT zero. Spent hours trying to figure out why my audio wasn't playing and realized it was because I never touched the pitch value and because it defaults to zero you get no sound.
I don't understand. So you design a custom Sound class that has most of the core data that a stock AudioSource already has. You then make a messy, obscure Inspector interface to interact with it - I mean, try to remove or add a sound from/to the middle of the list and see how that works... Only to just copy all the custom Sound data fields into the hidden AudioSources on Awake. What is the point of all of this other than to make everything obscure and ridiculously difficult to maintain once you have more than 2-3 sounds? Make an empty object that contains an AudioSource. Make a prefab out of it. Add a prefab instance for every sound you want to have; name your objects accordingly in the hierarchy. Make them all children of an AudioManager object. Take the whole object (with all children) and save them as a prefab as well. Just add this prefab to every scene. To add a new sound, simply go to prefab-editing mode and do what you must. No restrictions on deleting, adding, or changing the order of your sounds. No need to duplicate the data between a custom class and an actual AudioSource. You could even have separate sound lists per scene which is also super easy to set up. There are probably far better approaches to this as well, but this one is a straight up mess. Don't make lists of custom classes that you fully set up from the Inspector. Either load the data from a file (json, csv etc) or set it up with objects but don't go for the Inspector approach. Just the fact that you can't modify the order of your sounds alone should ring alarm bells. So should the data-duplicating. It's not viable for anything beyond a simple test scene. Like, I get that it's supposed to be a lesson for beginners but it's still a horrible approach.
I noticed that the car did not destroyed when you go forward in one way haha and u said make thing like when player go in one way for some time respawn one ticket on his way! Don't stop man your videos is helping a lot
Sorry, when I heard the first sound of the car, I had to stop the video, I couldn't stop laughing ... I was like "ok, it is going to show us a professional car sound, recorded with a good mic ...." and suddenly ... "fffrrruuuuuummmmMMM"
Okay, so I know this video is like a thousand years old and probably nobody will help me, but I did this tutorial (with success) on one of my projects in Unity 2018.4.10f and it worked. Now I'm doing the exact same thing in 2019.3.0f5 and it's not working at all. After hitting play the Audio Source is created for a sound I add in AudioManager, it has the proper amount of volume and it's not playing. When I manually add an Audio Source on some other object (like Brackeys did in the beginning of the video) it works and the sound is played properly. Can it be the fault of the Unity version, or am I just doing something wrong?
@@sulkingcrow7426 Yeah I figured it out a day later. The script had mininal value of pitch set to 0.1, but it was written .1 so I had missed the fact it's not 1. And Unity treats any value of pitch under 1 as if it was set to 0. But anyway, maybe someone will have a similar problem and get to learn quicker what was wrong, than I did ;)
MY GOD!!!! Your tutorials have come a long long way! i think you have cracked the key to making the best tutorials. Fast but explanatory. New Fav GameDev Channel
GREAT VIDEO! If I may add ... Since the audio "Name" is created when you add it to the Audio Manager, it might be best to add that name to the Audio Manager Enum. Like so... public enum AudioSounds { ShipEngineThruster = 0 , SomeOtherSoundName = 1, YetAnotherSountName = 2 } ...and then from any component using the Sound Manager that sound can be called by name like... audioManager.Play(AudioManager.AudioSounds.ShipEngineThruster); ...and in the Audio Manager's method to Play, we have... public void Play(AudioSounds name) { var s = FindAudioClip(name); if (s != null) { if (!s.source.isPlaying) { s.source.Play(); } } } private Sound FindAudioClip(AudioSounds name) { return Array.Find(sounds, sound => sound.name == Enum.GetName(typeof(AudioSounds), name)); } I hope you like this addition ;-) Love the work you do!!
I also wanted to just add, if folks are not familiar with what you were doing @13:10, this is called a singleton pattern (which I believe he mentions) and I would highly suggest anyone not familiar with this pattern to research it. In fact Jason W @ ua-cam.com/video/hQE8lQk9ikE/v-deo.html does a nice job of discussing this pattern as well as a few others often used in development. Thanks again for an awesome video.
You're one of the best online tutor I've came across. There's a lot to learn from you. Please don't stop teaching and keep doing this wonderful deed. God Bless You.
@@zoewalker2064 I have a different set up for my audio manager, but, looking back at this video, notice how he sets up the Play function. In the audio manager, Play takes a string input and finds the audio file with that name. The quickest solution to your problem is simply copy and paste the Play function, rename it Pause, and instead of writing "s.source.Play()" write "s.source.Pause()". Then when you want to pause a sound, just call the audio managers pause function and pass the audio files name as the input
Move these two lines of code: s.source.volume = s.volume; s.source.pitch = s.pitch; in Update() function in the AudioManager Script. Like this: private void Update() { foreach (Sound s in sounds) { s.source.volume = s.volume; s.source.pitch = s.pitch; } } That way you can adjust those values in real time while the game is running.
It's amazing how by watching these videos I learn about how little I know of coding. I love that feeling because it means there is so much more to learn! Thanks for the videos ":D
Anybody watching this now who doesn't want to mess around with the pitch each time, just change this in Sound.cs: public float pitch = 1f; This will default the pitch in the inspector to 1, that way you don't need to manually do it each time. You can also make the volume at a default like this.
Would you know how this would manage a PlayOneShot function? For instance, I have a gun that fires automatically and I don't want the sound to cut itself off without playing the full firearm sound effect.
if you cant hear sound in unity you might have to go into project settings - audio and uncheck "mute audio" or set master volume to 1 or in your game view at the top bar uncheck mute audio
Hey man! quick question, I've succesfully added the Audio manager , problem occurs when i try to combine the adding settings and slider control / mixer in my game . since yours creates a a new component .. how would i go about adding the mixer source on the component via script. Many thanks!
@@Averysh if you did the "Introduction to AUDIO in Unity" you need to add another component to the Audio Manager and Sound Class . in the Audio Manager : s.source.outputAudioMixerGroup = s.group; in the Sound : public AudioMixerGroup group; and then add in the group : "Master(MainMixer)" I did so and it worked ,hope it helps .
Fantastic tutorial! So useful! I had a problem in the Singleton code, because we had a "return" there, the sounds array was not being processed when the game went from level 1 to level 2 and I was getting a null reference error. I removed the "return" and everything worked fine. Thank you for sharing this.
Could you make a video about handling animation? For example: Weaponswitching in a fps You got an animation for putting down the current weapon. You also got an animation to pull out the second weapon. I implemented it one way, but it's not clean. I implemented a class, which starts a couroutine, which sets a bool in the animator of my current weapon, waits a second so it's finished, and then plays the next animation. I saw a lot of "tutorials" which try to create an animator-script. I don't think this apporach is good either.
if you refer to his weapon switching tutorial, that's not what I meant (not quite) I meant a tutorial about how to handle animation, I. e. import from a blend file, using mecanim. Like, wait for animations to finish using couroutines, implement clean animation transitions, the best way to implement animator controllers with blend trees, etc...
late reply, but for other people, make sure your pitch is above 0. 1 is the normal sound without any pitch changes. and volume needs to be up too! this worked for me
I started creating a game without having any c# knowledge last week based on your cube game. Now it has 7 proper levels, both keyboard and touch controls and a save file system.
This is such a nice video, I'm starting with Unity now so I dint get much of the code part but I can see how great the Audio Manager is. Thank you so much !
Use this function to adjust volume in runtime public void adjustvolume(float volume, string name) { sounds s = Array.Find(sounds, sound => sound.name == name); s.source.volume = volume;
@@AboA7ma11 I wanna use this but I'm stupid. What other class do you mean. You add this to the audiomanager right? I do have an array called sounds[ ] in my AudioManager script?
@@Somewhere_sometime_somehow yeah just paste this into your audio manger as a method and use it as any other method in your script. just make sure when you call it you input a value or a variable as your volume :D
How to adjust volume witch a slider in this AudioManager? Tried to add a new method for this but its dose not appears in functions while referencing AudioManager in a slider. public void AdjustVolume(float volume, string sound) { Sound s = Array.Find(sounds, item => item.name == sound); s.source.volume = volume; } How to make it work, please help ?
if I think about "Lets add some silence", I hear "Lets add some silence" but if I think about "Lets add some sounds", I hear "Lets add some sounds" Black magic
Excellent video! Thanks to you I learned more about classes and how they're used and about sound at the same time! multiple lessons in one video. Keep doing what you're doing your videos help out a lot.
if your voice doesn't sound(Maybe because of different version), add code, s.source.Play() foreach(sounds in sounds) { s.source = gameObject.AddComponent(); s.source.clip = s.clip; s.source.Play(); (Add This Code) s.source.volume = s.volume; s.source.pitch = s.pitch; } Hope you can help
Check out Brackeys How to Make a Settings Menu in Unity Video and he explains how you can make an options menu and change the value of the volume ua-cam.com/video/YOaYQrN1oYQ/v-deo.html
Well done tutorial, well done! Even has some more of that delicious advanced stuff, and helps a lot in understanding HOW TO MAKE CLEAN CODE which is indeed important
am am trying to implement sounds into my games since 2 months, but unity is somehow not playing any of my audio files. Does anyone have an idea on how ti fix this problem?
Oh my god thank you for this video, I have a platformer project due tonight and the audio manager was giving me so much trouble!! This made it way more bearable :)
Many Thanks for that video! You cover the most important parts of sound in such a short time and do it so precisely! Hope you will upload new videos in the future again.
i know this vid is quite old so probs no one will help, but how do you make it so that the music continues through the levels???? whenever i start a new level it starts the music from the beginning.
hey just wanna ask how come i cant edit my audio in my audio manager when i created a new clip inside of it with the audio put in it i cant edit the audio i can only edit the audio it its respective gameobject
@@dcry1003 I got the same problem too. Even if the idea of creating an audio manager is cool, don't forget that since this tutorial has been done unity has undergone many updates. The problem comes from the output. Right that you a new audio clip but if the output is not configured, you wont got any sound. To solve this I did this: -Go to Window-Audio and Add "AudioMixer". From the AudioMixer window(upper right corner) you should see "Exposed parameters" hover it and read the text to know what it means. -In your Sound.cs do this: add "public AudioMixerGroup output;" as variable. -In your AudioManager.cs: add"s.source.outputAudioMixerGroup = s.output;" to the for each loop that go through your "public Sound[] sounds". if you want to play your audio from the AudioManager(disable your game object that holds your audio clip to make sure that it works and add your audio to the AudioManager) then do as he did in the video, like this: void Start() { Play("arion"); } of course in AudioManager.cs. -Then in your "Assets" folder you should see some thing like "Master" that was not there. Drag it to your AudiManager output. Adjust pitch to at least 1, adjust volume and enter game mode to see how it works. But you should watch Brakeys tuto on how to create "settings" in games.
@@fulgencejuniorlohore854 thats gonna be pretty hard specially if im gonna disable my gameobject just to test if the audio clip would work since some of those clips are from my player gameobject like death sound, jump, dash, attack, etc. Maybe like create a empty gameobject put the audio clips there and parent it to the player gameobject maybe that could work?
In case you want to make a transition between two musics when you start another one, I made two coroutine to do that ;) the old music fade and the new one start progressively at the same time private Sound sound; public void Play(string name) { StopAllCoroutines(); if (sound != null) StartCoroutine(EndSound()); sound = Array.Find(sounds, s => s.name == name); if (sound == null) { Debug.LogWarning("Music " + name + " not found."); return; } StartCoroutine(StartSound()); } private IEnumerator EndSound() { AudioSource oldSound = sound.source; while (oldSound.volume > 0) { oldSound.volume -= 0.01f; yield return null; } oldSound.Stop(); } private IEnumerator StartSound() { sound.source.Play(); float volume = 0f; do { sound.source.volume = volume; volume += 0.01f; yield return null; } while (sound.source.volume
@@Viralfanatics Hey, oldSound is a copy of the music currently playing. This way, I can have the "old" music playing with the new one at the same time. And I can fade out the old sound to a volume of 0 and stop it to make a smooth transition between the 2 musics
@@AlineoRykkeErrel Oh Thanks. I want my music to fade out as I change scenes and in the next one I want another track to fade in. I have been trying to figure it out for hours now and I haven't been successfull yet :') Can you help me with that?
If code is not working for you anymore, change name variable to _name (both in AudioManager and Sound), thats because Unity recently made that name shows the name of the gameObject and that breaks the code.
In some scenes i want the music to stop using a trigger but i dont really know how to do that. Is there a way to have some kind of 'Cancelmusic' method that will pause certain sounds? If someone could just type a bit of code under this comment that would be awesome! 😄
@@hajperdev StopMusic.cs(10,19): error CS0103: The name 'Array' does not exist in the current context \StopMusic.cs(10,30): error CS0103: The name 'sounds' does not exist in the current context Cant get this to work at all. I dont understand why : AudioManager.instance.Stop("Smile"); does not work when: AudioManager.instance.Play("Smile"); works...
@@hajperdev problem is that you guys can code and we can not. So when nobody specifies that this code need to go into the SoundManager and not anywhere else, we get lost. I solved it by piecing things together and going through everything again. But it is often we newbies get confused because the well-meaning pro skips step that is second nature to them :)
Hi there everyone, I know that this is an old video, but what if I wanted to make a slider to control the volume if I made a settings menu. is there any way that I can hook this audio manager to an audio mixer?
okay I found a way : add : public AudioMixerGroup audioMixer; right before public Sound[] sounds; add : s.source.outputAudioMixerGroup = audioMixer; under the other s.source (volume, pitch, etc) assign your Master from the MainMixer in the editor and you're good !
Mano you are the champ! , would you happen to know how we can seperate the sounds into specifics , like fx , bg music etc , im thinking to create 3 sets of audio manager but that would be prolly a waste of code
Mikhail Opulencia you have to learn how the mixer works to do this. After looking this tutorial ua-cam.com/video/vOaQp2x-io0/v-deo.html you should be able to do it (I managed to create a fx volume slider and a music volume slider after this).
Brand new to your channel, and found this VERY informative! I'm a Pro Tool user making sfx and music, but was looking to learn some basic programming, and this was a nice gentle push into the realm of coding. Thanks, man!
don't stop teaching us.
please
wtf hhh heyyyy praying a long age for him
1000th Like
mr.MYSTERIOUS,YT, 2018
What a discourt
He is just awesome 😊😊😊
That feeling when you follow the tutorial, set everything up, tear your hair out for 3 hours wondering why the sound won't play... and then change the pitch from 0 to 1.
OMG THANK YOU.
The legend that saved me weeks of debuging- Grimm himself. Thanks dude!
@@maxplayerone9565 do we need to change [Range(0.1f, 3f)] of pitch variable to 1f,3f or what?
@@goodgamershow6505 i think you need to look at the Audio_Manager game object's script component
Your comment saved my hair. My hair thanks you.
If anyone is curious on how to stop playing a looping sound (maybe you want to switch bg music when you enter a new area) just use s.source.Stop (); You can just add a method to the Audio Manager like this
public void StopPlaying (string sound)
{
Sound s = Array.Find(sounds, item => item.name == sound);
if (s == null)
{
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
s.source.volume = s.volume * (1f + UnityEngine.Random.Range(-s.volumeVariance / 2f, s.volumeVariance / 2f));
s.source.pitch = s.pitch * (1f + UnityEngine.Random.Range(-s.pitchVariance / 2f, s.pitchVariance / 2f));
s.source.Stop ();
}
just call it like this (AudioManagerReferenceGoeshere).StopPlaying("sound string name");
Hey Nightcore Motion, I was just like you using s.source.Stop(), but it keeps giving me a NullReferenceException that the source equals to null, any idea on how to fix that?
You should actually write it at the place where you want to stop bgm 1 and play bgm 2.
if (blah blah){
FindObjectOfType ().StopPlaying ("GameplayMusic");
FindObjectOfType().Play("MenuMusic");
}
You sir, are a lifesaver!
I want to fade in the audio in start how to do that.
Wow thank you, I was trying for hours :D
Is the audio just you making noises
preprocessing with what, please?
hmm I think I remember using audacity to cut and convert some audio files Didnt know you could go much further. Thanks.
thank you for your kindess
NO!
...
Okay yeah :)
xD
This video is 4 years old, but it still legit!! I was struggling to find a way to manage game audio files until watching this. Many thanks to Brackeys!!
OK Guys, for some of you that will likely be getting a Null:Exception Error you might need to check that you aren't trying to Play() the sound in an AWAKE function on another object. I had this problem and for about a month I've been pushing it to the side and figured I'd get around to fixing it later when it became a little more important.
SO the problem I had was I used the same setup as in the tutorial, with another class I called Jukebox. This was to handle the current soundtrack playing and to allow players to cycle between other soundtracks in the game, like a Jukebox :D
Long story short, I was calling the following lines of code:
audioManager = FindObjectOfType();
audioManager.Play("music_metal_1");
within the Jukebox.Awake() function. This is a NO NO. The SOURCES aren't populated until later which means when I was trying to invoke them they hadn't actually been setup ;)
To get around this I simply called the same code in the Jukebox.Start() function which worked absolutely fine.
I hope this helps one other poor soul out there then I know my suffering was not in vain. This post isn't actively monitored so my query went almost 2 months without a single response. I hope this helps someone.
Never Give Up
Thank you!!!
@@Noobyoulus So basically, only use Awake() to initialize the class that method is in, and use Start() to get references from other classes, making sure the other classes have already been initialized by the Awake method.
Move these two lines of code:
s.source.volume = s.volume;
s.source.pitch = s.pitch;
from Awake to the Play function. That way you can adjust those values in real time while the game is running.
Brorger
How to control them all at once?
This doesn't work for me unfortunately.
So this works, except for the music. It doesn't seem to allow change at runtime for the currently playing sound.
@@DavidBixler How'd you do it?
This is by far one of the most straightforward and easy to follow tutorials I have watched in quite a while!
No it isnt
Isn't****
For anyone that's struggling with ArgumentNullExceptions when you switch scenes, it's because you're referencing the wrong AudioManager. For example, if you switch scenes, the AudioManager in the scene gets destroyed by the singleton code, but you call Find() on it instead of the singleton instance of the AudioManager from a previous scene. In other words, you shouldn't use Find() but rather AudioManager.instance when referencing the AudioManager.
In fact, if your game always starts at the main menu of your game then you only need one AudioManager instance in the main menu hierarchy, but I'd still recommend keeping AudioManagers in every scene.
Thank you ❤️❤️
omfg thank you so much bro
If you are new to coding and you arnt able to take classes I would suggest following along and paying attention with alot of beginner coding videos, it really does help, this channel its self is probably one of the best places to start.
Thank you a lot for this tutorial. I spent multiple days figuring out a way to keep the music nice and clean throughout scenes and after finding a somewhat working method, your tutorial is like a gift from the heavens (the internet heavens). Love your tutorials, love your good attitude, can't wait for the next one!
I have watched your videos for ages , learning a shit ton from them.
I just realized i never thanked you for the videos.
So.. THANK YOU! You are really motivating me through my struggles :D
Great video as always!
Overwatch lets go brother
Brackeys, without you I would be lost and stuck paying for a course. Thank you.
how are you now?
@@fv4202x My interest in game development has definitely decreased, but my skills and craft have definitely increased. I'm still planning on releasing games one day.
@@rigzmoviediaries654 good luck
@@rigzmoviediaries654 and how is that going for you now?
if I could marry a youtube channel it would probably be this. One of the best Brackeys videos I saw so far.
everything I needed about audio to my game is in this video.
@@GGOOSEO LMAO 69
A not an
@@spaghetti_bro3093 thank you
@@diegocrusius wut
Dude I love your direct style, you don't waste ANY time, and you trust the viewer to understand. Thank you so much.
It`s amazing that there are still people like you who keep helping others without wanting something for exchange. Love your vids. I wish you luck and don`t stop making this awesome tutorials.
yea about that...
I've rewatched this video so many times! Its such a good resource. Everytime I get to doing audio in a game I pull this up again for a refresher.
Love your sound effects :D
Haha, thanks! :)
+Brackeys Why Array.Find? Why not a dictionary with try get value instead?
Its just his voice
@@sfaezamin no shit sherlock
The more I watch the more I miss you man thanks for all the awesome tutorials.
Finally, I've been looking at a bunch of overly complicated sound managers that I can't seem to get to work, and then you come along and make it so much simpler and actually work.
Still using this 6 years later! Thanks!
me too
me too but.... "MissingReferenceException: The object of type 'AudioSource' has been destroyed but you are still trying to access it" and no I didn't alter the code
Very nice solution, if I may add one thing then instead of finding the gameobject reference every time you want to play a sound, you could write a static function in the audiomanager:
public static void PlaySoundStatic(string name)
{
if (instance == null)
{
Debug.Log("No audio manager ");
return;
}
instance.PlaySound(name);
}
Then we use the current active audiomanager to play the sound. Then you would write "AudioManager.PlaySoundStatic(string mySoundName);" to play a sound from anywhere.
Thanks! This helped me!
Quite possibly, one of the most useful Unity tutorials I have ever come across! Thanks Brackeys!
You rock man! Great programer, very organized and well explained! Thank you and never stop doing this videos!
You're an amazing instructor. Please never stop. Your videos are so clean and direct, and you cover information so well.
That didn't age well lol
MAKE SURE you give the pitch a value that is NOT zero. Spent hours trying to figure out why my audio wasn't playing and realized it was because I never touched the pitch value and because it defaults to zero you get no sound.
Thank you...This was my exact issue
This!☝
You are amazing! Years later and I'm still using your videos with great success. Thanks Brackeys.
1:36 i could say that you stick your mouth on the microphone and say "phooooophooooooophoooooo" xD
i love the way these guys made sounds, with their mouth
I don't understand. So you design a custom Sound class that has most of the core data that a stock AudioSource already has. You then make a messy, obscure Inspector interface to interact with it - I mean, try to remove or add a sound from/to the middle of the list and see how that works... Only to just copy all the custom Sound data fields into the hidden AudioSources on Awake.
What is the point of all of this other than to make everything obscure and ridiculously difficult to maintain once you have more than 2-3 sounds?
Make an empty object that contains an AudioSource. Make a prefab out of it. Add a prefab instance for every sound you want to have; name your objects accordingly in the hierarchy. Make them all children of an AudioManager object. Take the whole object (with all children) and save them as a prefab as well. Just add this prefab to every scene. To add a new sound, simply go to prefab-editing mode and do what you must. No restrictions on deleting, adding, or changing the order of your sounds. No need to duplicate the data between a custom class and an actual AudioSource. You could even have separate sound lists per scene which is also super easy to set up.
There are probably far better approaches to this as well, but this one is a straight up mess. Don't make lists of custom classes that you fully set up from the Inspector. Either load the data from a file (json, csv etc) or set it up with objects but don't go for the Inspector approach. Just the fact that you can't modify the order of your sounds alone should ring alarm bells. So should the data-duplicating. It's not viable for anything beyond a simple test scene. Like, I get that it's supposed to be a lesson for beginners but it's still a horrible approach.
Goddamn you are a genius
I like your method, it seems far less janky. Though given that this tutorial is 5 years old your hostility seems excessive.
I noticed that the car did not destroyed when you go forward in one way haha and u said make thing like when player go in one way for some time respawn one ticket on his way! Don't stop man your videos is helping a lot
You're the best !
Yes he is!
Oh yeah, oh yeah!
So useful. Going to miss the new uploads but hope your new adventures suit you well.
Sorry, when I heard the first sound of the car, I had to stop the video, I couldn't stop laughing ... I was like "ok, it is going to show us a professional car sound, recorded with a good mic ...." and suddenly ... "fffrrruuuuuummmmMMM"
Please Don't Stop Teaching Us Unity, We All Love You And Your Tutorial's. Thanks For Teaching Us Unity For Free
*you know, coding is easier than you think.*
You Know You ShOuLd TaKe ThIs OnlNe UniTy On UnEdEmy
@liberP lovPrimeNumbers YoU KnOw YoU dO
You know, coding is easier than you think when you just copy.
change my mind
@@synthjc5105 fucking hate that ad
YOU RESCUED ME FOR MY FIRST GAME JAM LOVE YOU FOREVER
If you are still looking to download and the link doesn't work!
Use this link.
old.brackeys.com/wp-content/FilesForDownload/AudioManager.zip
thank you so much
Thanks a lot
legend
The visuals and lighting and smoke are awesome looking in that little game.
My dude made a game about king Kai’s planet 😂😂 great vid man!
This video has just saved me. I have been struggling for a week with this issue, I should have known better and have come here earlier
Okay, so I know this video is like a thousand years old and probably nobody will help me, but I did this tutorial (with success) on one of my projects in Unity 2018.4.10f and it worked. Now I'm doing the exact same thing in 2019.3.0f5 and it's not working at all. After hitting play the Audio Source is created for a sound I add in AudioManager, it has the proper amount of volume and it's not playing. When I manually add an Audio Source on some other object (like Brackeys did in the beginning of the video) it works and the sound is played properly. Can it be the fault of the Unity version, or am I just doing something wrong?
I'm not sure if this is still relevant but I had the same problem and had to set the pitch to 1
@@sulkingcrow7426 Yeah I figured it out a day later. The script had mininal value of pitch set to 0.1, but it was written .1 so I had missed the fact it's not 1. And Unity treats any value of pitch under 1 as if it was set to 0. But anyway, maybe someone will have a similar problem and get to learn quicker what was wrong, than I did ;)
@@TigaLionessArt Thank god i see the comments here so can finish my game devoloping project for uni
@@sulkingcrow7426 thanks for posting this! I was trying to get this to work for over an hour. this tutorial is broken :(
@@jhgfghjfuzrtfchchghgf no problem! And it was a great tutorial, just a small (but important) part got looked over
MY GOD!!!! Your tutorials have come a long long way! i think you have cracked the key to making the best tutorials. Fast but explanatory. New Fav GameDev Channel
GREAT VIDEO! If I may add ...
Since the audio "Name" is created when you add it to the Audio Manager, it might be best to add that name to the Audio Manager Enum. Like so...
public enum AudioSounds
{
ShipEngineThruster = 0
,
SomeOtherSoundName = 1,
YetAnotherSountName = 2
}
...and then from any component using the Sound Manager that sound can be called by name like...
audioManager.Play(AudioManager.AudioSounds.ShipEngineThruster);
...and in the Audio Manager's method to Play, we have...
public void Play(AudioSounds name)
{
var s = FindAudioClip(name);
if (s != null)
{
if (!s.source.isPlaying)
{
s.source.Play();
}
}
}
private Sound FindAudioClip(AudioSounds name)
{
return Array.Find(sounds, sound => sound.name == Enum.GetName(typeof(AudioSounds), name));
}
I hope you like this addition ;-) Love the work you do!!
I also wanted to just add, if folks are not familiar with what you were doing @13:10, this is called a singleton pattern (which I believe he mentions) and I would highly suggest anyone not familiar with this pattern to research it. In fact Jason W @ ua-cam.com/video/hQE8lQk9ikE/v-deo.html does a nice job of discussing this pattern as well as a few others often used in development. Thanks again for an awesome video.
You're one of the best online tutor I've came across. There's a lot to learn from you. Please don't stop teaching and keep doing this wonderful deed. God Bless You.
What if I want to stop the theme music, for example when Player dies?
Nonno d' acciaio then destroy the audio manager
No, don't destroy the audio manager, why would he do that? Simply call the music using the audio manager and either pause it or stop it
lol
@@JasonEkonomakos how tho???
@@zoewalker2064 I have a different set up for my audio manager, but, looking back at this video, notice how he sets up the Play function. In the audio manager, Play takes a string input and finds the audio file with that name. The quickest solution to your problem is simply copy and paste the Play function, rename it Pause, and instead of writing "s.source.Play()" write "s.source.Pause()". Then when you want to pause a sound, just call the audio managers pause function and pass the audio files name as the input
all your videos are just so easy to use and straight to the point
thank you so much
Move these two lines of code:
s.source.volume = s.volume;
s.source.pitch = s.pitch;
in Update() function in the AudioManager Script.
Like this:
private void Update()
{
foreach (Sound s in sounds)
{
s.source.volume = s.volume;
s.source.pitch = s.pitch;
}
}
That way you can adjust those values in real time while the game is running.
genius
It's amazing how by watching these videos I learn about how little I know of coding. I love that feeling because it means there is so much more to learn! Thanks for the videos ":D
Anybody watching this now who doesn't want to mess around with the pitch each time, just change this in Sound.cs:
public float pitch = 1f;
This will default the pitch in the inspector to 1, that way you don't need to manually do it each time. You can also make the volume at a default like this.
Would you know how this would manage a PlayOneShot function? For instance, I have a gun that fires automatically and I don't want the sound to cut itself off without playing the full firearm sound effect.
if you cant hear sound in unity you might have to go into project settings - audio and uncheck "mute audio" or set master volume to 1 or in your game view at the top bar uncheck mute audio
Now how do we play these sounds from the Master mix that we made in your other video so we can decrease volume from the slider?
Thank you soooo much bracks you have no idea how many people have you helped!
Hey man! quick question, I've succesfully added the Audio manager , problem occurs when i try to combine the adding settings and slider control / mixer in my game . since yours creates a a new component .. how would i go about adding the mixer source on the component via script. Many thanks!
Did anyone figure this out yet?
@@Averysh if you did the "Introduction to AUDIO in Unity" you need to add another component to the Audio Manager and Sound Class .
in the Audio Manager :
s.source.outputAudioMixerGroup = s.group;
in the Sound :
public AudioMixerGroup group;
and then add in the group : "Master(MainMixer)" I did so
and it worked ,hope it helps .
@@kingdavegamess thx. You help me a lot.
@@serbaneduard5045
no problem
@@kingdavegamess Thx it works!!!!
Fantastic tutorial! So useful! I had a problem in the Singleton code, because we had a "return" there, the sounds array was not being processed when the game went from level 1 to level 2 and I was getting a null reference error. I removed the "return" and everything worked fine. Thank you for sharing this.
Could you make a video about handling animation?
For example: Weaponswitching in a fps
You got an animation for putting down the current weapon. You also got an animation to pull out the second weapon.
I implemented it one way, but it's not clean. I implemented a class, which starts a couroutine, which sets a bool in the animator of my current weapon, waits a second so it's finished, and then plays the next animation.
I saw a lot of "tutorials" which try to create an animator-script. I don't think this apporach is good either.
DerLink he literally did a vid a week ago on this
if you refer to his weapon switching tutorial, that's not what I meant (not quite)
I meant a tutorial about how to handle animation, I. e. import from a blend file, using mecanim.
Like, wait for animations to finish using couroutines, implement clean animation transitions, the best way to implement animator controllers with blend trees, etc...
I think maybe you should look into state machines @DerLink
Love the car voiceover work
I can't get any sound to come through. Even when directly copying and using your script. I can run the code with no error, but no sound either.
Got the same issue here, appears in the audio manager but does not play...
same
late reply, but for other people, make sure your pitch is above 0. 1 is the normal sound without any pitch changes. and volume needs to be up too! this worked for me
Was working until I set it as a prefab, then even trying again from the begining it didn't work, the pitch is well set also
try design your own, worked for me
Your tutorials are by far the best i could find out there 👍
The link to Audio Manager is not available anymore :((
old.brackeys.com/wp-content/FilesForDownload/AudioManager.zip
thanks mate :)
@@jeroeno_boy149 thx u so much
ik, and it hurts cuz i need it to find my issues. I CANT FIND ANOTHER VIDEO THAT SHOWS MY FKN PROBLEM!@!!!@@!@@!@!@!!!!@@!
Those lovely mouth-crafted audio clips...
Good timing, so far I have no audio in my program. This will now change :)
This is the only video that I've found that has successfully worked for me. Thank you!
0:36 "Let's add some silence"
As always your tutorials are remarkably helpful.
I can't hear the sounds when the object instantiated.
pitch needs to be at least .1f for sound to be made.
@@benbuehler4972 Thanks!
@@benbuehler4972 Thank you!!
@@benbuehler4972 Life saver, I've been going mad here
@@benbuehler4972 thank you legend
I started creating a game without having any c# knowledge last week based on your cube game. Now it has 7 proper levels, both keyboard and touch controls and a save file system.
And now it has sound as well
does anyone have a link to the audio manager download? looks like the link in the video is broken
same
@@DaMaLZ actually it works hahah
This is such a nice video, I'm starting with Unity now so I dint get much of the code part but I can see how great the Audio Manager is. Thank you so much !
Use this function to adjust volume in runtime
public void adjustvolume(float volume, string name)
{
sounds s = Array.Find(sounds, sound => sound.name == name);
s.source.volume = volume;
}
@@flaymes make sure the other class name is "sounds"
@@flaymes do adjustvolume("musicname", Slider.value);
Dude, you're a life saver. This is exactly what I was looking for. Thank you
@@AboA7ma11 I wanna use this but I'm stupid. What other class do you mean. You add this to the audiomanager right? I do have an array called sounds[ ] in my AudioManager script?
@@Somewhere_sometime_somehow yeah just paste this into your audio manger as a method and use it as any other method in your script. just make sure when you call it you input a value or a variable as your volume :D
You are the best teacher for coding games!!!
is there a way to cancel sounds(like music) by having some kind of stop method?
@@kadir9629 Thx
@@gdzantaf6144 StopMusic.cs(10,19): error CS0103: The name 'Array' does not exist in the current context.
@@gdzantaf6144 can you tell me how you did it?
4 years later and I am still using the same Audio Manager that I used in 2019 lol
How to adjust volume witch a slider in this AudioManager? Tried to add a new method for this but its dose not appears in functions while referencing AudioManager in a slider.
public void AdjustVolume(float volume, string sound)
{
Sound s = Array.Find(sounds, item => item.name == sound);
s.source.volume = volume;
}
How to make it work, please help ?
Did you ever figure it out?
Sorry man. Don't remember. That's was almost a year ago.(( I already probably lost the project where was using this thing.
@@maxageev3dart I got it ;) he made another video on an options menu that was pretty similar to what I was trying to do
damn I really like the way the Brackeys always give us the whole script and also teach us how to script yourself at the same time.
0:37 for some reason I heard "Lets add some silence" XDXDXD
if I think about "Lets add some silence", I hear "Lets add some silence"
but if I think about "Lets add some sounds", I hear "Lets add some sounds"
Black magic
Excellent video! Thanks to you I learned more about classes and how they're used and about sound at the same time! multiple lessons in one video. Keep doing what you're doing your videos help out a lot.
if your voice doesn't sound(Maybe because of different version), add code, s.source.Play()
foreach(sounds in sounds)
{
s.source = gameObject.AddComponent();
s.source.clip = s.clip;
s.source.Play(); (Add This Code)
s.source.volume = s.volume;
s.source.pitch = s.pitch;
}
Hope you can help
THANK YOU
Absolutely awesome tutorial, you go right to the point, speak clearly and very efficiently! Thank you very much!
How would I go about adding volume sliders to some of the sounds in my AudioManager?
Check out Brackeys How to Make a Settings Menu in Unity Video and he explains how you can make an options menu and change the value of the volume
ua-cam.com/video/YOaYQrN1oYQ/v-deo.html
Well done tutorial, well done! Even has some more of that delicious advanced stuff, and helps a lot in understanding HOW TO MAKE CLEAN CODE which is indeed important
am am trying to implement sounds into my games since 2 months, but unity is somehow not playing any of my audio files. Does anyone have an idea on how ti fix this problem?
im having the same issue
Have you the volume option at maximum ? Is unity no muted in your Volume mixer ?
Is your AudioSource near an AudioListenner ?
check the mute audio button isnt checked, its next to gizmos
Oh my god thank you for this video, I have a platformer project due tonight and the audio manager was giving me so much trouble!!
This made it way more bearable :)
The link for the download do not work anymore :(
i love how you are going over each topic. thank u sm
Nice sound effects
Many Thanks for that video!
You cover the most important parts of sound in such a short time and do it so precisely!
Hope you will upload new videos in the future again.
i know this vid is quite old so probs no one will help, but how do you make it so that the music continues through the levels???? whenever i start a new level it starts the music from the beginning.
just make the music go again when the scene open
@@shacharrodrigez7521 yh the thing is i want it to continue from where it left off in the last scene
idk if you already found it out but just watch everything after 12:11
@@cearaj405 ah ok thanks lol
You help me out buddy!!! Thanks for this tutorial! The Audio Manager is really useful!
hey just wanna ask how come i cant edit my audio in my audio manager when i created a new clip inside of it with the audio put in it i cant edit the audio i can only edit the audio it its respective gameobject
@@dcry1003 I got the same problem too. Even if the idea of creating an audio manager is cool, don't forget that since this tutorial has been done unity has undergone many updates. The problem comes from the output. Right that you a new audio clip but if the output is not configured, you wont got any sound.
To solve this I did this:
-Go to Window-Audio and Add "AudioMixer". From the AudioMixer window(upper right corner) you should see "Exposed parameters" hover it and read the text to know what it means.
-In your Sound.cs do this: add "public AudioMixerGroup output;" as variable.
-In your AudioManager.cs: add"s.source.outputAudioMixerGroup = s.output;" to the for each loop that go through your "public Sound[] sounds".
if you want to play your audio from the AudioManager(disable your game object that holds your audio clip to make sure that it works and add your audio to the AudioManager) then do as he did in the video, like this:
void Start()
{
Play("arion");
}
of course in AudioManager.cs.
-Then in your "Assets" folder you should see some thing like "Master" that was not there. Drag it to your AudiManager output. Adjust pitch to at least 1, adjust volume and enter game mode to see how it works.
But you should watch Brakeys tuto on how to create "settings" in games.
@@fulgencejuniorlohore854 thats gonna be pretty hard specially if im gonna disable my gameobject just to test if the audio clip would work since some of those clips are from my player gameobject like death sound, jump, dash, attack, etc. Maybe like create a empty gameobject put the audio clips there and parent it to the player gameobject maybe that could work?
In case you want to make a transition between two musics when you start another one, I made two coroutine to do that ;) the old music fade and the new one start progressively at the same time
private Sound sound;
public void Play(string name) {
StopAllCoroutines();
if (sound != null) StartCoroutine(EndSound());
sound = Array.Find(sounds, s => s.name == name);
if (sound == null) {
Debug.LogWarning("Music " + name + " not found.");
return;
}
StartCoroutine(StartSound());
}
private IEnumerator EndSound() {
AudioSource oldSound = sound.source;
while (oldSound.volume > 0) {
oldSound.volume -= 0.01f;
yield return null;
}
oldSound.Stop();
}
private IEnumerator StartSound() {
sound.source.Play();
float volume = 0f;
do {
sound.source.volume = volume;
volume += 0.01f;
yield return null;
} while (sound.source.volume
Idk if you'd see this or reply but what is old sound here. I am a beginner so I can't really understand that.
@@Viralfanatics Hey, oldSound is a copy of the music currently playing. This way, I can have the "old" music playing with the new one at the same time. And I can fade out the old sound to a volume of 0 and stop it to make a smooth transition between the 2 musics
@@AlineoRykkeErrel Oh Thanks. I want my music to fade out as I change scenes and in the next one I want another track to fade in. I have been trying to figure it out for hours now and I haven't been successfull yet :')
Can you help me with that?
Nevermind. I figured it out. Thanks a ton. Your comment helped a lot!!
@@Viralfanatics you're welcome haha, did you use DontDestroyOnLoad?
this is so clean....so FREAKNING CLEAN!!!!!!!
I can Play music but I can't stop it. How do i do that?
You would need to call Stop() on the audio clip.
@@GeorgeWulfers_88 how please? AudioManager.instance.Stop("MyMusic"); does not work.
If I need to know something I usually go to Brackeys first, love all of these videos.
Thanks, but the link to Audio Manager is dead now
If code is not working for you anymore, change name variable to _name (both in AudioManager and Sound), thats because Unity recently made that name shows the name of the gameObject and that breaks the code.
In some scenes i want the music to stop using a trigger but i dont really know how to do that. Is there a way to have some kind of 'Cancelmusic' method that will pause certain sounds? If someone could just type a bit of code under this comment that would be awesome! 😄
public void StopPlaying(string sound){
Sound s = Array.Find(sounds, item => item.name == sound);
if (s == null){
Debug.LogWarning("Sound: " + name + " not found!");
return;
}
s.source.volume = s.volume * (1f + UnityEngine.Random.Range(-s.volumeVariance / 2f, s.volumeVariance / 2f));
s.source.pitch = s.pitch * (1f + UnityEngine.Random.Range(-s.pitchVariance / 2f, s.pitchVariance / 2f));
s.source.Stop ();
}
Somebody posted it there, it's the same but you just stop instead of play
@@hajperdev StopMusic.cs(10,19): error CS0103: The name 'Array' does not exist in the current context
\StopMusic.cs(10,30): error CS0103: The name 'sounds' does not exist in the current context
Cant get this to work at all.
I dont understand why : AudioManager.instance.Stop("Smile"); does not work when: AudioManager.instance.Play("Smile"); works...
@@vrstepper1204 weird, I dont really remember how I made all of this but for modifying arrays im pretty sure you need to be using System libraries
@@hajperdev problem is that you guys can code and we can not. So when nobody specifies that this code need to go into the SoundManager and not anywhere else, we get lost. I solved it by piecing things together and going through everything again. But it is often we newbies get confused because the well-meaning pro skips step that is second nature to them :)
i love the fact he used is own voice going vroom into the car noise
Hi there everyone, I know that this is an old video, but what if I wanted to make a slider to control the volume if I made a settings menu.
is there any way that I can hook this audio manager to an audio mixer?
same
okay I found a way :
add :
public AudioMixerGroup audioMixer;
right before public Sound[] sounds;
add :
s.source.outputAudioMixerGroup = audioMixer;
under the other s.source (volume, pitch, etc)
assign your Master from the MainMixer in the editor and you're good !
Mano you are the champ! , would you happen to know how we can seperate the sounds into specifics , like fx , bg music etc , im thinking to create 3 sets of audio manager but that would be prolly a waste of code
Mikhail Opulencia you have to learn how the mixer works to do this. After looking this tutorial ua-cam.com/video/vOaQp2x-io0/v-deo.html
you should be able to do it (I managed to create a fx volume slider and a music volume slider after this).
Well knowing how the mixer works was not enough I had to do some c# research you'll find
Brand new to your channel, and found this VERY informative! I'm a Pro Tool user making sfx and music, but was looking to learn some basic programming, and this was a nice gentle push into the realm of coding. Thanks, man!