public bool MusicOnBool;
// This method will toggle the MusicOnBool and saves it to playerprefs
public void ToggleButton()
{
MusicOnBool = !MusicOnBool; // sets it to its opposite state
music.SetActive(MusicOnBool);
// Below we save the bool (which is either a 1 or 0) to "MusicOn"
PlayerPrefs.SetInt("MusicOn", MusicOnBool ? 1 : 0); // condition ? true : false, so if the condition is true, itll be 1, otherwise it'll be 0.
music.SetActive(MusicOnBool);
}
// This method can be called wherever, such as the Start() method in your script and basically it'll load the saved int and check to see if it's 1 which would be true. So if 0, it'll be false, if 1, it'll be true.
public void Load()
{
MusicOnBool = PlayerPrefs.GetInt("MusicOn", 1) == 1;
// EX: MusicOnBool = 0 == 1 will result in false
music.SetActive(MusicOnBool);
}