Continuous muting issue on all scenes

G’day all.

I am new to Unity just to let you know guys. I have an issue with mute button. It does work but only until I am not going to get back to settings menu (where it is located). So it work on all scenes (stay muted) but once I click back to Settings it somehow un click it self and sound is back on.

My code…

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class MusicManager : MonoBehaviour {

bool muted;

void Update()
{
if (muted) {

AudioListener.volume = 0F;

} else if (!muted) {

AudioListener.volume = 1F;

}
}

public void Mute()
{
muted = !muted;

Any idea? I believe the solution is simple, I just can get it.

hi,

so that settings menu, is it separate scene or in same scene? are you setting mute value or calling that Mute() there everytime it opens?

also i think you don’t need to continuously run that mute check with Update() loop,
can just add it inside that Mute() function to set once,

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

ps. can add code snippets in posts

Hi. Thank you for your advise. Well, Settings menu is in separate scene. I am calling that Mute toggle every time. I tried your idea on Mute function with no success.

how do you set mute on in that settings menu scene? using PlayerPrefs or DontDestroyOnLoad() or others?

Hi. I am calling it thru On Value Changed (Boolean) in a Toggle Script

oh yeah, i think that one fired everytime the button is initialized / shown or something similar…?

had same issue myself before, you could test that by putting Debug.Log() message inside Mute() function to see if it happens.

i had to do this with code, when onValueChanged is called, check if the toggle is actually on or off.
(or you could just pass the toggle component as reference to check, no need to use the AddListener line then

// at Start()
GetComponent<Toggle>().onValueChanged.AddListener(delegate {this.SetMode();});

...
public void SetMode()
        {
            if (GetComponent<Toggle>().isOn==true)
            {
               // do stuff when toggle is enabled
            }else{      
               // do stuff when toggle is disabled
         }
        }

Awesome. Thank you very much…