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. 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.
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
}
}