- Home /
Question by
Haythamcrc · May 20, 2016 at 10:46 AM ·
c#playerprefstoggle
Save a Toggle Group state?
Hi, Here is my current music mute code, which works perfectly, but can't get the state of the toggle to stay as it should when resetting the scene.
Thanks to anyone who can help!
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PauseMenuScriptToggle : MonoBehaviour
{
const float DefaultVolumeLevel = 1.0f;
bool isMute;
public bool isOn;
void Start()
{
audio.volume = PlayerPrefs.HasKey("CurVol") ? PlayerPrefs.GetFloat("CurVol") : DefaultVolumeLevel;
}
public void MusicMute ()
{
if(isMute == true){
Debug.Log("Music On");
audio.volume = 1.0f;
isMute = false;
}
else
{
Debug.Log("Music Off");
isMute = true;
audio.volume = 0;
}
PlayerPrefs.SetFloat("CurVol", audio.volume);
PlayerPrefs.Save();
}
}
Comment
Answer by KazYamof · May 20, 2016 at 11:04 AM
I imagine that you have something like: Toggle myToggle on some script, referencing the UIToggle on your canvas. Well, just save another prefs with his name and state when unload the scene, and read this pref when reloading it,
myToggle.isOn = PlayerPrefs.HasKey("MyUIToggle") ? PlayerPrefs.GetBool("MyUIToggle") : false;
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Differences between mobile save/load data 1 Answer