Hi All,
I have a script which toggles sound on/off based on playerprefs. For some reason when game is started I always get 0 value for the sound setting in SoundInit method. Cant figure out why?
Is it because Im calling it on Start method?
using UnityEngine;
using System.Collections;
using UnityEngine.Analytics;
public class SoundManager : MonoBehaviour {
public GameObject SoundOnBtn, SoundOffBtn;
// Use this for initialization
void Start () {
SoundInit ();
}
public void SoundInit() {
int settingSound = PlayerPrefs.GetInt ("soundsetting");
Debug.Log ("settingSound " +settingSound);
if (settingSound == 0) {
//Music is ON
SoundOnBtn.SetActive (true);
SoundOffBtn.SetActive (false);
AudioListener.volume = 1f;
} else {
//Music is OFF
SoundOnBtn.SetActive (false);
SoundOffBtn.SetActive (true);
AudioListener.volume = 0f;
}
}
public void SoundToggle() {
int settingSound = PlayerPrefs.GetInt ("soundsetting");
if (settingSound == 0) {
//Music is ON
Debug.Log ("settingSound set to 1");
PlayerPrefs.SetInt ("soundsetting",1);
SoundOnBtn.SetActive (false);
SoundOffBtn.SetActive (true);
AudioListener.volume = 0f;
Analytics.CustomEvent("SoundOFF");
Debug.Log (PlayerPrefs.GetInt ("soundsetting"));
} else {
//Music is OFF
PlayerPrefs.SetInt ("soundsetting",0);
SoundOnBtn.SetActive (true);
SoundOffBtn.SetActive (false);
Debug.Log ("settingSound set to 0");
AudioListener.volume = 1f;
Analytics.CustomEvent("SoundON");
}
PlayerPrefs.Save ();
}
}
↧