Adding BGM And On/Off Sound Button - Easy Unity Tutorial

Тәжірибелік нұсқаулар және стиль

In this unity tutorial, you'll learn how to add background music and make an on/off sound button for our game. Background music enables players to immerse themselves in the world at hand and brings players gaming experience to the next level. Adding a on/off sound button in your game settings gives players the option to play with or without sound.
➤Adventure On The Road: assetstore.unity.com/packages...
Timestamps:
0:00 Intro
0:18 Background Music Scripting
1:18 Sound Button UI Setup
1:50 Sound Manager Scripting
5:14 Conclusion
#Hooson #Unity3D
Play my first mobile game: Bot Attack - 2D Top-down shooter
➤Google Play: play.google.com/store/apps/de...
➤App Store: Coming soon
➤Follow my Instagram: / hoosontech

Пікірлер: 88

  • @hoosontech
    @hoosontech3 жыл бұрын

    Hi guys! I have a tutorial on how to add a Volume Slider! Do check it out if you would like to add a Volume Slider instead of an On/Off Sound Button! kzread.info/dash/bejne/q4t3qsONp7nInM4.html

  • @losantosw

    @losantosw

    2 жыл бұрын

    Thank you very much, I was already wondering where I would find another video where I would put in addition to the audio on and off options, a volume adjustment. Now I'll go to the next video to learn the other part lol

  • @abderrazeksimmou8753
    @abderrazeksimmou87533 жыл бұрын

    Script using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class SoundManager : MonoBehaviour { [SerializeField] Image soundOnIcon; [SerializeField] Image soundOffIcon; private bool muted = false; // Start is called before the first frame update void Start() { if(!PlayerPrefs.HasKey("muted")) { PlayerPrefs.SetInt("muted", 0); Load(); } UpdateButtonIcon(); AudioListener.pause = muted; } public void OnButtonPress() { if (muted == false) { muted = true; AudioListener.pause = true; } else { muted = false; AudioListener.pause = false; } Save(); UpdateButtonIcon(); } private void UpdateButtonIcon() { if(muted == false) { soundOnIcon.enabled = true; soundOffIcon.enabled = false; } else { soundOnIcon.enabled = false; soundOffIcon.enabled = true; } } private void Load() { muted = PlayerPrefs.GetInt("muted") == 1; } private void Save() { PlayerPrefs.SetInt("muted", muted ? 1 : 0); } }

  • @HOJ2

    @HOJ2

    11 ай бұрын

    thx bro!

  • @phellippemottarini
    @phellippemottarini Жыл бұрын

    WOW, easy, simple and PERFECT. Thank you!

  • @VeronicaJessie
    @VeronicaJessie3 жыл бұрын

    Thank you so much. You make this so simple and easy to understand!

  • @jasperhwong3341
    @jasperhwong33413 жыл бұрын

    Straight to the point, thanks a lot!

  • @gabrielulate
    @gabrielulate3 жыл бұрын

    Awesome video dude, keep it up!

  • @KRaJWanders
    @KRaJWanders5 ай бұрын

    the best one out there Had been looking for this for some days now. Thanks a lot bro, you saved me

  • @crispychips9868
    @crispychips9868 Жыл бұрын

    Thank you for making these videos they're a really big help

  • @eddiea6569
    @eddiea65693 жыл бұрын

    Thanks for the tutorial, it helped!

  • @zamharir_
    @zamharir_3 жыл бұрын

    Thanks for this amazing tutorial, it really helped

  • @Blazikennnn
    @Blazikennnn3 жыл бұрын

    thanks dude, it really helped me for my thesis project :D

  • @rodsal
    @rodsal3 жыл бұрын

    Amazing Tutorial! You got a new sub :)

  • @kingstar9995
    @kingstar99952 жыл бұрын

    Hi i really like your channel man and your editing you also the explenation that you give us while you are working on it i really love it

  • @yncor9756
    @yncor97562 жыл бұрын

    thanks dude! you're the best ;)

  • @CutyDina
    @CutyDina Жыл бұрын

    Very useful, thanks!

  • @saiganeshr1436
    @saiganeshr14362 жыл бұрын

    It really helped me alot today and in future, Thanks for this wonderful information

  • @chermainehoo214
    @chermainehoo2143 жыл бұрын

    Great video!!

  • @megakumalasarihrp6785
    @megakumalasarihrp67852 жыл бұрын

    Thanks a lot, your tutorial work 👍

  • @dazniputra
    @dazniputra3 жыл бұрын

    It worked. Thanks

  • @makofujihara5533
    @makofujihara55332 жыл бұрын

    By the gods, you are a life saver T^T

  • @alphaplus4969
    @alphaplus49693 жыл бұрын

    thank you very much. it helped me a lot

  • @ahmedkhabar
    @ahmedkhabar2 жыл бұрын

    Thanks A lot Man, New Subscribe ❤❤❤

  • @remybeaumont8089
    @remybeaumont80892 жыл бұрын

    Merci mec, tu m'as permis d'avoir 10%

  • @syedmuhammadalishah5684
    @syedmuhammadalishah56842 жыл бұрын

    Thanks Nice Work

  • @hdggfhjb6768
    @hdggfhjb67683 жыл бұрын

    I like your tutorial. Because have good UI & TEXT on video for extra teach

  • @luck8762
    @luck8762 Жыл бұрын

    Thank you so much!!!!!

  • @VIMES_
    @VIMES_4 ай бұрын

    thank you!

  • @indiestudio23
    @indiestudio232 жыл бұрын

    Thank You Brother...

  • @hasanboyhakimov9144
    @hasanboyhakimov91442 жыл бұрын

    Thank you bro

  • @ake.studio
    @ake.studio2 жыл бұрын

    thanks for video

  • @sadksazdag851
    @sadksazdag8512 жыл бұрын

    thank you bro

  • @Asif-ze5hp
    @Asif-ze5hp9 ай бұрын

    Great

  • @MegaTVscreen
    @MegaTVscreen2 жыл бұрын

    @Hooson how would you add pressedState for the mute buttons with this solution you showed us in the video (changing sprites)? You know, to indicate to the users that the button was pressed (e.g. changing the sprite's color to indicate that). Right now utilizing your method I am unable to find a solution on how to do apply a pressed effect to the mute/unmute button. Any suggestions? Thanks 🙂

  • @oterobot
    @oterobot Жыл бұрын

    Thank you

  • @miguelespeso401
    @miguelespeso401 Жыл бұрын

    Congratulations It's an amazing video. I managed to do what I was looking for to see your video. I would be very grateful if you activated the subtitles in Spanish to fully understand your logic, I like how you explain your video, but I don't fully understand it.

  • @enjoylearning8585
    @enjoylearning85852 жыл бұрын

    best one

  • @atokosto
    @atokosto2 жыл бұрын

    🔥

  • @CurtisJensenGames
    @CurtisJensenGames2 жыл бұрын

    Tip for anyone wanting to mute all sound effects: write a script for the audio listener, not the audio source

  • @JuanFranco-mq7js
    @JuanFranco-mq7js2 жыл бұрын

    Thanks

  • @UpcomingChris
    @UpcomingChris2 жыл бұрын

    The PlayerPrefs part of this isn't working like I'd expect, if I close the game, change scene or something, the music will always start on regardless if I turned it off or not and I don't know why, is there something I'm missing?, code is identical, isn't it mean to be persistently saved?

  • @nguyentrunghieu6865
    @nguyentrunghieu68652 жыл бұрын

    I like video

  • @losantosw
    @losantosw2 жыл бұрын

    Hi @Hooson, I replicated this code to make something to turn sounds and effects on and off. The problem is that in the effects, where I set another audio, when I click the button it plays the audio sound again. I don't understand why this behavior, since they are two different scripts. Can you help me?

  • @neowb8495
    @neowb84953 жыл бұрын

    I have a problem, when I changed scenes the icon goes back to OnMusic, but last time i left it, it is on OffMusic icon

  • @saswatamohanta1023
    @saswatamohanta10232 жыл бұрын

    how would i do it to turn off only music and not sound effects? Pausing the audio listener turns off all the sounds

  • @rizwanshaikh8175
    @rizwanshaikh81752 жыл бұрын

    save system is working properly the only issue is when I go to option menu it restart the setting (when I set muted and go to option menu its unmuted)

  • @FranyxD
    @FranyxD3 жыл бұрын

    i want to in one scene in specify dont play that music how can i do it?

  • @weejunjie93
    @weejunjie932 жыл бұрын

    I know this a long time vid, but just to ask why at the unity On Click Part, I cant add Function to it

  • @benjib20
    @benjib205 ай бұрын

    yo im having an issue where the on and off are on the screen at the same time. what do i do?

  • @shahpranay1236
    @shahpranay12363 жыл бұрын

    what is the getkeydown replacement for android can u help please?

  • @vladimirvetchinnikov6916
    @vladimirvetchinnikov69163 жыл бұрын

    Please, help me. What should I write here? UpdateButtonIcon(); AudioListener.pause = muted; if I use AudioMixer public void MusicButtonOnOff() { if (mutedMusic == false) { mutedMusic = true; Mixer.audioMixer.SetFloat("Music", -80); } else { mutedMusic = false; Mixer.audioMixer.SetFloat("Music", 0); }

  • @talismanskulls2857
    @talismanskulls2857 Жыл бұрын

    I like the concept but i am using sliders to adjust audio source. how do i mute those/

  • @shivaprasadkm5423
    @shivaprasadkm5423 Жыл бұрын

    Hello i got problem that is, whenever the game starts the sound off button is enabled its not saving as set in last game,i got same issue in slider also (as u pinned in comment video).any suggestions.

  • @xcade2652
    @xcade265210 ай бұрын

    Hi the buttons weren't showing up is there a reason to it why isnt it showing when i hit play?

  • @F.Fifie.
    @F.Fifie.2 жыл бұрын

    why mine didnt have music if i change to different scene? please helppp

  • @rizwanshaikh8175
    @rizwanshaikh81752 жыл бұрын

    after going back to sound button it restart and play the music

  • @adsikaros9611
    @adsikaros96115 ай бұрын

    but it also muted the sfx how to make it only stop the bgm

  • @IlyasMMVII
    @IlyasMMVII7 ай бұрын

    Backgound Music: using UnityEngine; public class BackgroundMusic : MonoBehaviour { private static BackgroundMusic backgroundMusic; void Awake() { if (backgroundMusic == null) { backgroundMusic = this; DontDestroyOnLoad(gameObject); } else { Destroy(gameObject); } } } SoundManager: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class SoundManager : MonoBehaviour { [SerializeField] Image soundOnIcon; [SerializeField] Image soundOffIcon; private bool muted = false; // Start is called before the first frame update void Start() { if (!PlayerPrefs.HasKey("muted")) { PlayerPrefs.SetInt("muted", 0); Load(); } else { Load(); } UpdateButtonIcon(); AudioListener.pause = muted; } public void OnButtonPress() { if (muted == false) { muted = true; AudioListener.pause = true; } else { muted = false; AudioListener.pause = false; } Save(); UpdateButtonIcon(); } private void Load() { muted = PlayerPrefs.GetInt("muted") == 1; } private void Save() { PlayerPrefs.SetInt("muted", muted ? 1 : 0); } private void UpdateButtonIcon() { if (muted) { soundOnIcon.enabled = false; soundOffIcon.enabled = true; } else { soundOnIcon.enabled = true; soundOffIcon.enabled = false; } } }

  • @texnofest
    @texnofest3 жыл бұрын

    Please make this for vibrate

  • @sheejarajendran137
    @sheejarajendran1373 жыл бұрын

    Ok bro it's working but there is a problem for me, when I mute my music and go to another scene and come back again but the audio is playing, it's not saving How to solve this

  • @sheejarajendran137

    @sheejarajendran137

    3 жыл бұрын

    Please replay bro

  • @mcpedab7442

    @mcpedab7442

    3 жыл бұрын

    Ya I was having the same issue. I had to do a bit of tweaking to the Sound Manager script but it now works. Here's the code using UnityEngine; using UnityEngine.UI; public class SoundManager : MonoBehaviour { [SerializeField] GameObject soundonImage; [SerializeField] GameObject soundoffImage; private bool muted = false; void Start() { muted = PlayerPrefs.GetInt("muted") == 1; if(muted == false) { soundonImage.SetActive(true); soundoffImage.SetActive(false); } else { soundonImage.SetActive(false); soundoffImage.SetActive(true); } if (PlayerPrefs.HasKey("muted")) { Load(); } else { PlayerPrefs.SetInt("muted",1); Save(); } AudioListener.pause = muted; } private void UpdateButtonIcon() { if(muted == false) { soundonImage.SetActive(true); soundoffImage.SetActive(false); } else { soundonImage.SetActive(false); soundoffImage.SetActive(true); } } public void OnButtonPress() { if (muted == false) { muted = true; AudioListener.pause = true; } else { muted = false; AudioListener.pause = false; } UpdateButtonIcon(); Save(); } private void Load() { muted = PlayerPrefs.GetInt("muted") == 1; } private void Save() { PlayerPrefs.SetInt("muted", muted ? 1 : 0); } } Just copy paste the script and reassign the values and it should work like a charm.

  • @sheejarajendran137

    @sheejarajendran137

    3 жыл бұрын

    @@mcpedab7442 ok thank you bro 😃

  • @densaldanha

    @densaldanha

    2 жыл бұрын

    @@mcpedab7442 Absolute life saver, thanks a lot!

  • @muraterguzel8795

    @muraterguzel8795

    2 жыл бұрын

    @@mcpedab7442 Thanks! This is working

  • @neomylusie2017
    @neomylusie20173 жыл бұрын

    why I have errors like, error CS0117: 'PlayerPrefs' does not contain a definition for 'Haskey' ??

  • @hoosontech

    @hoosontech

    3 жыл бұрын

    Can I take a look at your script? You can post the codes here

  • @gomolemomelela6052

    @gomolemomelela6052

    3 жыл бұрын

    k needs to be a capital letter, so it needs to be HasKey, not Haskey

  • @yncor9756

    @yncor9756

    2 жыл бұрын

    HasKey

  • @syedsadiq8631
    @syedsadiq86313 жыл бұрын

    Make a mobile game in unity plz

  • @arindameika3805
    @arindameika38053 жыл бұрын

    that's very helpful thanks, btw, do you know how to add different BGM in different scenes?

  • @chsufyansharif2405

    @chsufyansharif2405

    3 жыл бұрын

    yes i knowm

  • @arindameika3805

    @arindameika3805

    3 жыл бұрын

    @@chsufyansharif2405 Do you have the tutorial?

  • @chsufyansharif2405

    @chsufyansharif2405

    3 жыл бұрын

    @@arindameika3805 No i dont have tutorial

  • @himanshukharkar8886

    @himanshukharkar8886

    3 жыл бұрын

    make your bgm script static and use dontdestroyonload() method, thats all

  • @AYANKHAN-eh6ve

    @AYANKHAN-eh6ve

    2 жыл бұрын

    just add this to your very first scene, and the music will play all along until we exit the game.

  • @daniionas2888
    @daniionas2888 Жыл бұрын

    using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class SoundManager : MonoBehaviour { [SerializeField] Image SoundONNIcon; [SerializeField] Image SoundOFFIcon; private bool muted = false; void Start() { if (!PlayerPrefs.HasKey("muted")) { PlayerPrefs.SetInt("muted", 0); Load(); } else { Load(); } UpdateButtonIcon(); AudioListener.pause = muted; } public void OnButtonPress() { if (muted == false) { muted = true; AudioListener.pause = true; } else { muted = false; AudioListener.pause = false; } Save(); UpdateButtonIcon(); } private void UpdateButtonIcon() { if(muted == false) { SoundONNIcon.enabled = true; SoundOFFIcon.enabled = true; } else { SoundONNIcon.enabled = false; SoundOFFIcon.enabled = true; } } private void Load() { muted = PlayerPrefs.GetInt("muted") == 1; } private void Save() { PlayerPrefs.SetInt("muted", muted ? 1 : 0); } }

  • @moshi1002

    @moshi1002

    Жыл бұрын

    thanks mate

  • @SheReact
    @SheReact11 ай бұрын

    Bro I don't understand

  • @sillyninja65
    @sillyninja653 жыл бұрын

    fuck me i hate coding.

  • @wimpykiddd
    @wimpykiddd3 жыл бұрын

    I have trouble "Assets\script\SoundManager.cs(16,13): error CS0103: The name 'PlayerPref' does not exist in the current context" here the code void Start() { if (!PlayerPrefs.HasKey("muted")) { PlayerPref.SetInt("muted", 0); Load(); } else { Load(); } UpdateButtonIcon(); AudioListener.pause = muted; }

  • @hoosontech

    @hoosontech

    3 жыл бұрын

    There is a slight typo error in line 16. It should be "PlayerPrefs.SetInt("muted", 0);". You're missing a letter "s" in your PlayerPref

  • @wimpykiddd

    @wimpykiddd

    3 жыл бұрын

    @@hoosontech Hahaha XD my bad sorry, but thanks for the reply

  • @PixluxGames
    @PixluxGames Жыл бұрын

    Thanks

Келесі