unity Toggle value code example

Example 1: toggle unity c#

private bool togl;//this is either true or false

        if (Input.GetKey(KeyCode.A)) //can have whatever statment you want here
        {							 
            togl = !togl; //this is what changes the bool
        }

Example 2: how to save toggle values unity

public Toggle Toggle;

    private void Start() {
        if ((PlayerPrefs.GetInt("ToggleBool") == 1)) {
            Toggle.isOn = true
        }
        else {
            Toggle.isOn = false;
        }
    }

    private void Update() {
        if (Toggle.isOn == true) {
            PlayerPrefs.SetInt("ToggleBool", 1);
        }
        else {
            PlayerPrefs.SetInt("ToggleBool", 0);
        }