how to make sound settings button?
I am making a basic button, but the point is that i want it to be on-off button. So i have a settings scene and there should be button with picture: "sound on", and pressing this button will change it to "sound off" picture (I have made pictures) and will turn sounds off in every scene. Can somebody help me with this coding? What I have tried is: using UnityEngine; using System.Collections; using UnityEngine.UI;
public class uiManager : MonoBehaviour {
bool onoff;
public void Music(){
if (onoff) {
AudioListener.volume = 0;
bool onoff = !onoff;
} else if (!onoff) {
AudioListener.volume = 1;
bool !onoff = onoff;
}
}
Answer by Toon_Werawat · Mar 23, 2016 at 12:24 PM
Everytime scene change. All script will Destroy.
But you can make it not Destroy or Save to Setting
1.DontDestroyOnLoad
public class uiManager : MonoBehaviour
{
void Awake()
{
DontDestroyOnLoad(this.gameObject);
}
bool onoff;
public void Music()
{
//Your code
}
}
2.Save your setting. And load it later when change scene
public class uiManager : MonoBehaviour
{
void OnLevelWasLoaded(int level)
{
onoff = PlayerPrefs.GetInt("Music") == 1;
}
bool onoff;
public void Music()
{
//Your code
PlayerPrefs.SetInt("Music", onoff ? 0 : 1);
PlayerPrefs.Save();
}
}
Optinally: I can shrink your code. From this
if (onoff)
{
AudioListener.volume = 0;
bool onoff = !onoff;
}
else if (!onoff)
{
AudioListener.volume = 1;
bool !onoff = onoff;
}
To this
onoff = !onoff;
AudioListener.volume = onoff ? 0 : 1;
Thank you for your help, but for some reason, it doesn't work :( It doesn't change the sound off :(