- Home /
Question by
BluetheFox · Sep 30, 2017 at 07:51 PM ·
playerprefstogglebool
playerprefs dont load for menu toggle
using UnityEngine;
using UnityEngine.UI;
public class Instructor_toggle : MonoBehaviour
{
private bool togglevalue;
private Toggle toggle;
// Use this for initialization
void Start()
{
toggle = GetComponent<Toggle>(); //get the toggle
togglevalue = toggle.isOn; //get the toggle's value
toggle.onValueChanged.AddListener((value) => { changeinstructor(value); });//add listener to the toggle
togglevalue = PlayerPrefsX.GetBool("instructor"); //get the setting
}
public void changeinstructor(bool thevalue)
{
Toolbox.instructor_enabled = thevalue; //set the actual instructor to changed value
PlayerPrefsX.SetBool("instructor", togglevalue); //set the setting
PlayerPrefs.Save(); //save the setting
}
}
the above code doesn't work properly. I have this script attached to a toggle in my pause menu ui, and the problem im having is that the value always reverts to it's default, off, whenever i exit the game and re-enter the game. I want this toggle to be persistent across game sessions. what puzzles me is that this is set up in exactly the same way as an audio volume slider on the same menu which works perfectly as i want it to.
Comment
Your answer
Follow this Question
Related Questions
Toggle OnValueChanged ignore on start 2 Answers
Changing Toggle UI's value (Checked or Unchecked) 5 Answers
Toggle bool with keypress -2 Answers
Save a Toggle Group state? 0 Answers
Using an Enum for GUI.Toggle? 1 Answer