0

Hi I have been researching a lot about this issue but i couldn't find the best solution of my problem.

So the problem is that my background music starts playing.I have got two buttons."On" and "Off". I want to mute the music so when i click my mute button,it from "On" goes to "Off" and the music stops.But when i change the scene and go back it returns to the button with the text "On" but the music is still stopped. So my question is only how to save the changed button without going back by default? Here is my script:

public GameObject on;
public GameObject off;
public string sound;

Soundtrack music;
static bool isMuted;


void Awake()
{
    SoundCheck();


}
void Start()
{
    music = FindObjectOfType<Soundtrack>();
}
public void Off()
{
    if (isMuted == false)
     {
    on.gameObject.SetActive(false);
    off.gameObject.SetActive(true);
    music.s.Stop();
    isMuted = true;
    PlayerPrefs.SetString("Sound", "muted");
     }
}
public void On()
{    if(isMuted==true)
   {
    on.gameObject.SetActive(true);
    off.gameObject.SetActive(false);
    music.s.Play();
    isMuted = false;
    PlayerPrefs.SetString("Sound", "enabled");
   }
}
public void SoundCheck()
{
    sound = PlayerPrefs.GetString("Sound");

    if (sound == "enabled")
    {
        isMuted = false;
    }
    else if (sound == "muted")
    {
        isMuted = true;
    }
}

}

Abdu Omar
  • 35
  • 7
  • Perhaps not related to the problem at hand, but it looks like you've got a big problem with your if statements: multi-line code you want executed in the if statement should be enclosed in curly braces - otherwise, only the first line after the if statement will be conditionally executed. For example, in `Off()`, the only line that will be conditionally executed will be `on.gameObject.SetActive(false);`. The rest of the lines will be executed every time the method is called, no matter what. I recommend you get in the habit of writing curly braces with every if statement you write. – Serlite Dec 12 '16 at 02:34
  • Oh yeah i knew that. It doesn't work either when i put the curly braces on the whole code in the function.Now i must have inadvertently just erased them while writing this post.But thanks for your attention :) .Now i will correct it for future readers. – Abdu Omar Dec 12 '16 at 02:38
  • Looks like a duplicate. Please look [here](http://stackoverflow.com/a/41073496/3785314) – Programmer Dec 12 '16 at 07:24

2 Answers2

0

it's easy. First of all it's much better to save bool type in your PlayerPrefs instead ofstring. But to solve your issue all you have to do it's add is

public void SoundCheck() { sound = PlayerPrefs.GetString("Sound"); if (sound == "enabled") { isMuted = false; } else if (sound == "muted") { isMuted = true; } on.gameObject.SetActive(isMuted); off.gameObject.SetActive(!isMuted); }

zhekazheka
  • 111
  • 4
0

You need change SoundCheck() call in Start function not Awake! the Start() will call when you back the scence,but Awake not.

Btw:

  1. Should bool for save PlayerPrefs,
  2. Think a button and change sprite to Sprite On and Sprite Off can better.
Geo Wesley
  • 103
  • 8