Version: 2019.1 (switch to 2018.3 or 2017.4)
LanguageEnglish
  • C#

Toggle.isOn

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public bool isOn;

Description

Return or set whether the Toggle is on or not.

Return whether the Toggle is on or off. See the delegates tutorial page for more information about delegates.

//Attach this script to a Toggle GameObject. To do this, go to Create>UI>Toggle.
//Set your own Text in the Inspector window

using UnityEngine; using UnityEngine.UI;

public class Example : MonoBehaviour { Toggle m_Toggle; public Text m_Text;

void Start() { //Fetch the Toggle GameObject m_Toggle = GetComponent<Toggle>(); //Add listener for when the state of the Toggle changes, and output the state m_Toggle.onValueChanged.AddListener(delegate { ToggleValueChanged(m_Toggle); });

//Initialize the Text to say whether the Toggle is in a positive or negative state m_Text.text = "Toggle is : " + m_Toggle.isOn; }

//Output the new state of the Toggle into Text when the user uses the Toggle void ToggleValueChanged(Toggle change) { m_Text.text = "Toggle is : " + m_Toggle.isOn; } }

Did you find this page useful? Please give it a rating: