Tổng Hợp

Simple Sound Manager (Unity Tutorial)

Let’s learn how to play Sounds and create a simple Sound Manager class to handle playing any Sound we want in 2D and 3D.

GameAssets class video

If you have any questions post them in the comments and I’ll do my best to answer them.

See you next time!

Grab the game bundle at

Get the Code Monkey Utilities at

#unity3d #tutorial #unity2d


Hello and welcome, I am your Code Monkey and here you will learn everything about Game Development in Unity 2D using C#.

I’ve been developing games for several years with 7 published games on Steam and now I’m sharing my knowledge to help you on your own game development journey.

You can see my games at www.endlessloopstudios.com


– Website:
– Twitter:
– Facebook:

Nguồn: https://vinhtrinh.com.vn

Xem thêm bài viết khác: https://vinhtrinh.com.vn/tong-hop/

Tổng Hợp
VNG sập server diện rộng, cả hệ thống game Zing và Zalo mất truy cập (Đã tìm ra nguyên nhân)
Tổng Hợp
One Piece Tập 481 – Ace Được Cứu Thoát! Râu Trắng Ra Mệnh Lệnh Thuyền Trưởng Cuối Cùng – Đảo Hải Tặc
Tổng Hợp
Chèn chữ vào video, Hướng dẫn ghép chữ vào video bằng Premiere
  • Do you normally play your sounds through code or instantiate a prefab?

  • Really nice tutorial and exactly what I was looking for. One question – is there really no more efficient way in Unity/C# to find the AudioClip than looping through the array? I know it's only a small array but anything with O(n) complexity makes me uncomfortable!

  • Dude. Do you have any idea how good an instructor you are? I've been watching coding/unity videos for years. You are highly organized, extremely concise, and just a smart organizer of code. Level: Elite.

  • GabanGaban

    Author Reply

    Stopped the video at 1:52, 3 lines and the code is already gore, i can't believe a professional gamedev spread bad codings practices like that

  • How do you adjust audioclip volume with this method?

  • I have noticed for my ui button sometime the click sound is playing twice some time like normal just one time sometime not playing at all. What is the problem? may be the scene loads fast that the sound is not playing. Can you tell how to fix this

  • W HcW Hc

    Author Reply

    Great Tutorial!

  • W HcW Hc

    Author Reply

    Hello, I want to know how can we loop an Audio clip when the player is hitting a collider, if you have an idea of how can we do this using your script, I am very interested to hear your answer! Thank you for your reply!

  • Very good tutorial! Thanks Code Monkey! 😉

  • How do you save the sound to PlayerPrefs to be accessed in a different script? I already have my audio manager written for my sliders, but cannot seem to transfer the playerprefs for my sound effects. Here is my code:

    using UnityEngine;

    using UnityEngine.UI;

    public class AudioManager : MonoBehaviour


    private static readonly string FirstPlay = "FirstPlay";

    private static readonly string BackgroundPref = "BackgroundPref";

    private static readonly string SoundEffectsPref = "SoundEffectsPref";

    private int firstPlayInt;

    public Slider backgroundSlider, soundEffectsSlider;

    private float backgroundFloat, soundEffectsFloat;

    public AudioSource backgroundAudio;

    public AudioSource[] soundEffectsAudio;

    void Start()


    firstPlayInt = PlayerPrefs.GetInt(FirstPlay);

    if (firstPlayInt == 0)


    backgroundFloat = .25f;

    soundEffectsFloat = .75f;

    backgroundSlider.value = backgroundFloat;

    soundEffectsSlider.value = soundEffectsFloat;

    PlayerPrefs.SetFloat(BackgroundPref, backgroundFloat);

    PlayerPrefs.SetFloat(SoundEffectsPref, soundEffectsFloat);

    PlayerPrefs.SetInt(FirstPlay, -1);




    backgroundFloat = PlayerPrefs.GetFloat(BackgroundPref);

    backgroundSlider.value = backgroundFloat;

    soundEffectsFloat = PlayerPrefs.GetFloat(SoundEffectsPref);

    soundEffectsSlider.value = soundEffectsFloat;



    public void SaveSoundSettings()


    PlayerPrefs.SetFloat(BackgroundPref, backgroundSlider.value);

    PlayerPrefs.SetFloat(SoundEffectsPref, soundEffectsSlider.value);


    void OnApplicationFocus(bool inFocus)


    if (!inFocus)





    public void UpdateSound()


    backgroundAudio.volume = backgroundSlider.value;

    for(int i = 0; i < soundEffectsAudio.Length; i++)


    soundEffectsAudio[i].volume = soundEffectsSlider.value;




  • I cannot initiate the GameAsset, "The object you want to initiate is null"…. Confusing.

  • Lost me at 2:18 pulling out an entire script out of nowhere… man if it's essential to the tutorial why send us back to another tutorial 2 minutes in?

  • i keep triggering the sound in the sound manager and it just creates an empty game object with an audio source with no audio clip, have i done something wrong?

  • Thanks for the video! I have a question, I want to play a sound for a button using the SoundManager and then change scene, but the scene changes very fast and doesn't wait for the sound finish. How can I add a WaitForSeconds on this SoundManager? I tried with corutines but can't add it to static class.

  • Great tutorial, it is what I was looking for.

    Thank you for sharing your knowledge, you are awesome.

    I wil recommend you to my friends, and… if I will make someday game, I will add u in credits, u helped a lot, not just in this video.

    Stay safe!

  • SergSerg

    Author Reply

    How should I implement this with Unity mixer? So I can get audio settings?

  • Thank you so much <3

  • nice

  • Anybody getting compilers errors on the foreach loop. Mine is saying it can't convert audio manager game asset

  • how do i make if enemy is chaseing me, sound start

  • Ok so the debug.logError. I get that error… Cant figure out what im doing wrong

  • I'm sorry if I sound too rude, but this code is breaking too many good-programming rules that my heart is in pain.

  • Great video and explanation, helps me so much!!

  • This is a great tutorial, but I'm getting an error. "ArgumentException: The Object you want to instantiate is null. UnityEngine.Object.CheckNullArgument(System.Object arg, System.String message)"

  • you rulz!

  • I have modified your script to make easier volume and pitch setup with sliders: In the GameAsset script add 2 public float volume and pitch(with sliders),than modify this SoundManager methods like this:
    public static void PlaySound(Sound sound) {

    if (CanPlaySound(sound)) {

    if (!oneShotGameObject) {

    oneShotGameObject = new GameObject("Sound");

    oneShotAudioSource = oneShotGameObject.AddComponent<AudioSource>();





    private static AudioClip GetAudioClip(Sound sound,AudioSource audioSource) {

    foreach (GameAssets.SoundAudioClip soundAudioClip in GameAssets.i.soundAudioClips) {

    if (soundAudioClip.sound == sound) {

    audioSource.volume = soundAudioClip.volume;

    audioSource.pitch = soundAudioClip.pitch;

    return soundAudioClip.audioClip;



    Debug.LogError("Sound " + sound + " not found!");

    return null;


    Now if u go in the GameAsset object you can adjust volume and pitch 🙂

  • i look for audio manager for me at all sites and youtube and i never found what i need,
    but you really are the best coder
    I have seen ,
    can you tell how i make a button to change sprite audio on and audio off like the image
    but with your code


    and how to save Audio for every time i open the game by PlayerPrefs
    and thank you.

  • The best tutor! Thanks you!

  • What if I attatched multiple audio sources components to game objects like component for the sword and play it from sword script when the attack happened I'm another audio source for the rute player when he moves ?

  • Please can you show us how to make the same scene It looked very fancy

  • O oO o

    Author Reply

    Thanks a lot for your information rich tutorials. I hope you keep continuing.
    Instead of defining a dictionary and hardcoding delay timer in a switch-case for each clip, I defined two variables in the SoundAudioClip class, one for defining the delay value in the editor, one for storing last play time. then defined a CanPlay method in SoundAudioClip class.

  • I like your cool tutorials man please keep the best world

  • Can you make a video about the infinite background(like the grass in this video)? I just want to know how to make something like this.

  • Make a discord server!!

  • Can i shoot a Ray cast out to an object instead of a straight line..the game I'm doing is a flight sim were the aim sight isn't always straight ahead cos of the camera offset..if you know what I mean

  • Come here, cause your add

  • Nice tutorial but can you make a tutorial on tower defence game

  • Wow finally the first time in my life youtube showed me a good add because i was searching for a game programmer and boom finally found someone thanks to a add wow 🙂