Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to use a toggle to mute sound

Discussion in 'UGUI & TextMesh Pro' started by Zyie, Jan 24, 2015.

  1. Zyie

    Zyie

    Joined:
    Jan 12, 2015
    Posts:
    22
    Currently I have my toggle to On Value Change make AudioSource.enabled, which does turn the music off.

    However when I die in the game the level gets reloaded causing the toggle to go back to its default state and then i am unable to turn the music back on.

    So what is the best way to make a music toggle and have the state of the toggle be remembered on load?
     
  2. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,331
  3. Zyie

    Zyie

    Joined:
    Jan 12, 2015
    Posts:
    22
  4. Athomield3D

    Athomield3D

    Joined:
    Aug 29, 2012
    Posts:
    193
  5. Zyie

    Zyie

    Joined:
    Jan 12, 2015
    Posts:
    22
  6. Athomield3D

    Athomield3D

    Joined:
    Aug 29, 2012
    Posts:
    193
    you just have to put the bool value in a database (playerprefs) and load it and assign it's value to the toggle.
     
  7. bobbylee

    bobbylee

    Joined:
    Aug 15, 2013
    Posts:
    49
    PlayerPrefs maybe?

    PlayerPrefs.SetInt("Music", checkmusic ? 1 : 0);
    checkmusic = PlayerPrefs.GetInt("Music") == 1 ? true : false;

    Used in your toggle OnValueChanged?
     
  8. Zyie

    Zyie

    Joined:
    Jan 12, 2015
    Posts:
    22
    Would you be able to tell me how to do that?

    Im sorry for not getting it, i've only been doing this for a few days
     
  9. Athomield3D

    Athomield3D

    Joined:
    Aug 29, 2012
    Posts:
    193
    bobbylee is doing it the right way ;)
     
  10. Zyie

    Zyie

    Joined:
    Jan 12, 2015
    Posts:
    22
    So where would I put bobbylee's code?
     
  11. bobbylee

    bobbylee

    Joined:
    Aug 15, 2013
    Posts:
    49
    PlayerPrefs, Set/Get and value change.
     
    Last edited: Jan 25, 2015
  12. Zyie

    Zyie

    Joined:
    Jan 12, 2015
    Posts:
    22
    This turns off the music the first time it is used, however when the level is reloaded pressing the toggle does nothing
     
  13. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,331
    AudioListener.volume persists across scenes, but your toggle state doesn't, so this means you have to set the initial 'Toggle.isOn' value based on the current AudioListener.volume value.

    I would make a simple script called 'VolumeToggle' or similar, and have two methods: OnEnable, and OnValueChanged. I would then make it so OnEnable reads the AudioListener.volume, and sets the Toggle.isOn value based on this. Then I would hook up my toggle's OnValueChanged event to the OnValueChanged method, and have it set the AudioListener.volume value. You should now have a perfectly working toggle button, and you can even add extra code into that OnValueChanged to play a click sound or something.
     
  14. Philomorph

    Philomorph

    Joined:
    Jun 6, 2014
    Posts:
    3
    I've never done this, but a friend of mine said he uses the "DontDestroyOnLoad" property on a global control object where his scripts live so that it survives loading different levels. This supposedly maintains the value of his mute button across levels. Sorry I don't know more about it but maybe it will be useful information.
     
  15. cdm89

    cdm89

    Joined:
    Mar 29, 2014
    Posts:
    3
    I love when developers are super vague. Its almost as if they dont want you to know how to do it, or expect beginners to learn everything on their own.

    So here is an explanation:

    Create three script called MusicController, MainMenuController, and GamePreferences
    GamePreferences:
    inside this script you can set the difficulty of your game and other settings such as music - am only going to show you how to do the music
    make a public static string (I called mine IsMusicOn and set it to "IsMusicOn"
    Then Create a public static int GetMusicState() and in the {} return PlayerPrefs.GetInt(GamePreferences.IsMusicOn);
    since you set a getter you also need a setter so....
    public static void SetMusicState(int state){
    PlayerPrefs.SetInt(GamePreferences.IsMusicOn, state);
    }
    MusicController:
    create a static instance of the music controller
    create a variable of type audiosource
    create an awake funciton
    create a function that will act as a singleton - this will allow your music to play between scenes. (this is something you can google and find how to code it)

    now for the music player:
    Make a method that takes a bool
    public void PlayMusic(bool play)
    in this method you want to check if play is true, if it is and if the audioSource is not playing then start playing the audiosource. something like this if(play) { if(!audiosource.isPlaying) audiosource.Play} else {if (audiosource.isplaying) audiosource.Stop()}

    MainMenuController
    Create a button for your musicbutton
    create an array to hold the images for your on and off buttons

    and create a method for the button itsself
    if gamepreferences.getmusicstate == 0 (false/off) then set the gamepref set music state to 1 (true/ on) else if gamepref getmusicstate == 1 then set the music state to 0 (in this same statement you also want to make an instance of the musiccontroller and set playmusic to true or false accordingly. You also want to set the image accordingly by doing musicbutton.image.sprite = musicIconArray[0] (or [1]) .... I know this was kinda rushed but let me know if you have any more questions. Happy to help!

     
  16. Clownsy

    Clownsy

    Joined:
    Jan 6, 2015
    Posts:
    1
    I did this in my game. Put this script in SoundManager.
    (You should first create the ui buttons)
    public GameObject audioOnIcon;
    public GameObject audioOffIcon;

    and then:

    public void ToggleSound()
    {
    if (PlayerPrefs.GetInt ("Muted", 0) == 0)
    {
    PlayerPrefs.SetInt ("Muted", 1);
    } else
    {
    PlayerPrefs.SetInt ("Muted", 0);
    }
    SetSoundState ();
    }

    private void SetSoundState()
    {
    if (PlayerPrefs.GetInt ("Muted", 0) == 0)
    {
    AudioListener.volume = 1;
    audioOnIcon.SetActive (true);
    audioOffIcon.SetActive (false);
    }
    else
    {
    AudioListener.volume = 0;
    audioOnIcon.SetActive (false);
    audioOffIcon.SetActive (true);
    }
    }

    Good Luck!
     
  17. THE_GAMER_20081

    THE_GAMER_20081

    Joined:
    Oct 25, 2020
    Posts:
    1
    Whats the "audioOnIcon" and the "audioOffIcon"
     
  18. dlawless74

    dlawless74

    Joined:
    Nov 9, 2020
    Posts:
    2
    that would be the UI buttons