- Home /
Mute volume / sound problem
okay my problem is when i press the mute button it works fine and unmute is fine but the problem is ive set it with a picture button but what happens is when I mute it and go to the next scene and go back again the volume is still muted but the button for it is for not muted okay here's my code
void OnGUI(){
if(yMute){
if (GUI.Button(new Rect(100, 10, 100, 50), "",Y))
{
AudioListener.volume = 0.0F;
muted = true;
nMute = true;
yMute = false;
}
}
if(nMute){
if (GUI.Button(new Rect(100, 10, 100, 50), "",N))
{
AudioListener.volume = 1.0F;
muted = false;
nMute = false;
yMute = true;
}
}
}
void Start () {
yMute = true;
}
// Update is called once per frame
void Update () {
if (yMute == true) {
nMute = false;
}
else {
nMute = true;
}
}
}
so what I need is when i press my mute button the picture will change for the unmute and when i press it again the unmute will change for the mute picture it works fine but when i go to the next scene and go back here again the volume is muted but my button is for not muted so how can i fix that ? please help me thank you
Answer by VincentDB · Sep 15, 2014 at 11:06 AM
The problem is that your using multipe variables to detect if user have alredy muted or not the sound. And you reset to false yMute variable at Start, so when you back to the scene, yMute is set to true, but nMute keep his old value.
Use only one variable would be better, something like:
void OnGUI()
{
if(!isMuted)
{
if (GUI.Button(new Rect(100, 10, 100, 50), MuteImage))
{
isMuted = true;
AudioListener.volume = 0.0F;
}
}
else
{
if (GUI.Button(new Rect(100, 10, 100, 50), unMuteImage))
{
isMuted = false;
AudioListener.volume = 1.0F;
}
}
}
thankyou but, still wont work it wont even make the volume go to 0 i dont know why and when I press it its good the button is changing but when i go to the next scene with the un$$anonymous$$uteImage and go back its already changed to $$anonymous$$uteImage.....
Try to save a value.
void OnGUI()
{
if(PlayerPrefs.GetInt("Is$$anonymous$$uted") == 0)
{
if (GUI.Button(new Rect(100, 10, 100, 50), $$anonymous$$uteImage))
{
PlayerPrefs.SetInt("Is$$anonymous$$uted", 1);
AudioListener.volume = 0.0F;
}
}
else
{
if (GUI.Button(new Rect(100, 10, 100, 50), un$$anonymous$$uteImage))
{
PlayerPrefs.SetInt("Is$$anonymous$$uted", 0);
AudioListener.volume = 1.0F;
}
}
}
worked like a charm thank you so much made my life easier :)
the only problem now is when the game restarts it goes back with sound
If you want keep player settings for next restart game, you have to save this setting by:
PlayerPrefs.Save();
You can use this function by only one call on the function:
void OnApplicationQuit() { PlayerPrefs.Save(); }
Np and good continuation !