Hi!
I have a Singleton MusicManager what offers sounds and music to a game, menus, buttons etc. I tested today Unity3d 4.6 new Toggle-button and somehow it is working. If I turn sounds off in my option menu and go to play a game and come back to option menu, the image is back in default state (music on). I am using my MenuManager.cs and it is calling singleton Musicmanager, How can I prevent this? Is there way to tell Unity that don’t destroy and instantiate options-scene again when jumping between scenes or could I somehow programmatically change the state/image of the toggle-button to be right?
EDIT: The weirdest thing is that according to the Unity Log, when I come second time to Option screen, it goes automatically MusicManagerSingleton and MenuManager’s ToggleMusic() methods even if I haven’t touch the whole button? My usecase is that when I go first time to options and mute the music, everything is working fine, BUT when I come back there without touching the button,music starts and mute/sounds Toggle button is in state true.
EDIT2
Unity calls automatically ToggleMusic()-method as well and that was the reason. I added this line and everything started to work:
if (toggle.isOn != MusicManagerSingleton.Instance.musicOn())
public class MenuManager : MonoBehaviour {
public bool musicOn;
public AudioClip musicClip;
public UnityEngine.UI.Toggle toggle;
public void Start ()
{
toggle = GameObject.Find("MusicToggle").GetComponent<UnityEngine.UI.Toggle>();
toggle.isOn = MusicManagerSingleton.Instance.musicOn ();
Debug.Log ("0. START() MenuManager musicOn in ToggleMusic()--->" +toggle.isOn);
}
public void ToggleMusic() {
MusicManagerSingleton.Instance.toggleMusic ();
//toggle.isOn = MusicManagerSingleton.Instance.musicOn ();
Debug.Log ("1. AFTER TOGGLING MenuManager musicOn in ToggleMusic()--->" +toggle.isOn);
Debug.Log ("2. AFTER TOGGLING MenuManager musicOn in ToggleMusic()--->" +MusicManagerSingleton.Instance.musicOn ());
}
}
EDIT2 NOW THIS IS WORKING
***************MenuManagerSingleton.toggleMusic()*************************
public void ToggleMusic() {
if (toggle.isOn != MusicManagerSingleton.Instance.musicOn()) {
MusicManagerSingleton.Instance.toggleMusic ();
}
}
Thanks!
Tumpula