change slider to toggle button in script

i have the following class used for ui slider. now i need to use it in another project where i use ui toggle button.

how may i change the functionality ?

public class MusicManager : MonoBehaviour
{
    [SerializeField]
    private AudioMixer masterMixer;

    [SerializeField]
    private Slider musicSlider;

    [SerializeField]
    private Slider sfxSlider;

    public static AudioClip coinPickUpSound;
    public static AudioClip groundEnemySpikeSound;
    public static AudioClip playerJump;

    static AudioSource audioSrc;


    private void Start()
    {
        musicSlider.value = PlayerPrefs.GetFloat("musicVolume", 0);
        sfxSlider.value = PlayerPrefs.GetFloat("sfxVolume", 0);

        coinPickUpSound = Resources.Load<AudioClip>("coinPickUp");
        groundEnemySpikeSound = Resources.Load<AudioClip>("groundEnemySpike");
        playerJump = Resources.Load<AudioClip>("playerJump");

        audioSrc = GetComponents<AudioSource>()[1];
        audioSrc = GetComponents<AudioSource>()[2];
        audioSrc = GetComponents<AudioSource>()[3];
    }

    public static void PlaySound(string clip)
    {
        audioSrc.PlayOneShot(coinPickUpSound);
    }
    public static void PlayEnemySpikeSound(string clip)
    {
        audioSrc.PlayOneShot(groundEnemySpikeSound);
    }
    public static void PlayerJumpSound(string clip)
    {
        audioSrc.PlayOneShot(playerJump);
    }
    public void SetMusicVolume(float volume)
    {
        masterMixer.SetFloat("musicVolume", volume);
    }

    public void SetSfxVolume(float volume)
    {
        masterMixer.SetFloat("sfxVolume", volume);
    }

    private void OnDisable()
    {
        float musicVolume = 0;
        float sfxVolume = 0;

        masterMixer.GetFloat("musicVolume", out musicVolume);
        masterMixer.GetFloat("sfxVolume", out sfxVolume);

        PlayerPrefs.SetFloat("musicVolume", musicVolume);
        PlayerPrefs.SetFloat("sfxVolume", sfxVolume);
        PlayerPrefs.Save();

    }
}

@hoshyarvgds To confirm, you want to change audio volume with a toggle button?

1 Like

Good day to you, yes i need to have a toggle button to mute and unmute music volume. thanks for any help.

Please help

Steps to success:

  • isolate the stretch of code that takes the Slider control input and uses it for a volume level

  • replace that stretch of code with something that:

----> observes the state of the Toggle control (on or off)
----> based on that value sets either 0 for OFF or whatever volume level you want for ON
----> pass that decided value onto wherever the Slider used to send it.

It’s all data flows… flow with the data. Identify the data, tap into it, keep it flowing.

public class MusicManager : MonoBehaviour
{
    //[SerializeField]
    //private AudioMixer masterMixer;

    [SerializeField]
    private Toggle musicToggleButton;

    [SerializeField]
    private Toggle sfxToggleButton;



    private void Start()
    {
        musicToggleButton.onValueChanged.AddListener(delegate { SetMusicVolume(musicToggleButton); });
        sfxToggleButton.onValueChanged.AddListener(delegate { SetSfxVolume(sfxToggleButton); });

    }


    // saving and loading music state
    public void SetMusicVolume(Toggle toggleValue)
    {
        Debug.Log(toggleValue.isOn);

    }

    public void SetSfxVolume(Toggle toggleValue)
    {
        Debug.Log(toggleValue.isOn);

    }

}

this works, but the music is not muted when clicking on toggle button. and also i need to save the toggle button state.

any tips pleased.

That’s because you never mute it, you just print out the value of the toggle.

You can save the state in PlayerPrefs.

1 Like

any idea how to create PlayerBool ? which i use here? playerprefs int?
which command is for toggle button music mute? is it Is On property?

  public void SetMusicVolume(Toggle toggleValue)
    {
        Debug.Log(toggleValue.isOn);
        musicToggleButton.isOn = !musicToggleButton.isOn;

    }

is this correct?

woof! its all crashed. also i need to use Audimixer for this.

Up to you how you store it, int between 0 and 1 could work for a bool.

Nope absolutely not that’s going to reinvoke onValueChanged and cause an infinite loop. You need to uncomment your ‘masterMixer’ and mute/unmute that depending on the value of musicToggleButton.isOn.

1 Like
   [SerializeField]
    private AudioMixer masterMixer;

    [SerializeField]
    private Toggle musicToggleButton;

    [SerializeField]
    private Toggle sfxToggleButton;



    private void Start()
    {
        musicToggleButton = GameObject.Find("music_toggle_btn").GetComponent<Toggle>();
        sfxToggleButton = GameObject.Find("sfx_toggle_btn").GetComponent<Toggle>();


    }


    // saving and loading music state
    public void SetMusicVolume(bool toggleValue)
    {
        Debug.Log(toggleValue);

        if (toggleValue)
        {
            AudioListener.volume = 0;
        }
        else
        {
            AudioListener.volume = 1;
        }

    }

    public void SetSfxVolume(bool toggleValue)
    {
        Debug.Log(toggleValue);

    }

this is muting and unmuting the music but there are some problems:
i need to use Audiomixer

i need to save the state

how can i do these please?

Load the value from PlayerPrefs in your Start method, reuse SetMusicVolume for this.

1 Like

which PlayerPrefs?" Int?

how do you call mute command of Audiomixer Group via c# please?

Probably.

1 Like

i need to use PlayerPrefs.int in Start method for loading?

where in SetMusicVolume() method , do you mean i write Playerprefs.int set ? to save?

Use PlayerPrefs#GetInt() in your Start method to load the value, and use PlayerPrefs#SetInt() to set it.

1 Like

here is the most important question:

how to reference and link mute property of Auidmixer Group to PlayerPrefs#SetInt() - PlayerPrefs#GetInt() in the script?

because i use exposed audiomixer groups to have music and sfx isolated,

i mean in start method , ok i use PlayerPrefs#GetInt() . now what parameter need to be assigned to this int expreson?

The parameters are as follows:
The first parameter dictates where the value is stored, in your case it could be “Mute”. The second parameter is the default value, so if PlayerPrefs doesn’t contain anything called “Mute” then it’ll default to your set value. Which will be either 0 or 1 in your case.

1 Like

you mean something like this?

  private void Start()
    {
        musicToggleButton = GameObject.Find("music_toggle_btn").GetComponent<Toggle>();
        sfxToggleButton = GameObject.Find("sfx_toggle_btn").GetComponent<Toggle>();

        int musicEnabled = PlayerPrefs.GetInt("musicVolume", 1);

        if (musicEnabled == 1)
        {
            musicToggleButton.isOn = true;
        }
        else
        {
            musicToggleButton.isOn = false;
        }
    }