- Home /
Changing Toggle UI's value (Checked or Unchecked)
I can't seem to find a way wherein we can change the value of the Toggle UI.
Setting it to checked or unchecked. I wonder if it's really impossible or I'm just missing something.
Why I need it?: I'm developing a 3D Game where there are ImageEffects such as Bloom, Motion Blur, and Antialiasing. And I have a pause menu with settings that toggles those ImageEffects to turn it on or off.
Somehow, I wanted it to be saved in PlayerPrefs so once an ImageEffect is off, it will stay off once you open the game again.
Answer by JaredHD · Feb 20, 2017 at 06:44 PM
You use .isOn to set the value. E.G
using UnityEngine;
using UnityEngine.UI;
public class HelpSomeone : MonoBehaviour
{
GameObject inGameToggle;
private void Start()
{
inGameToggle = GameObject.Find("Toggle Name");
}
//Use buttons linked to this
public void ChangeValueToTrue()
{
inGameToggle.GetComponent<Toggle>().isOn = true;
}
//Use buttons linked to this
public void ChangeValueToFalse()
{
inGameToggle.GetComponent<Toggle>().isOn = false;
}
}
Ohhh! Why didn't I see that! This is perfect. Thank you! :)
I know that this is an old question but I still wanted to address one more issue. When setting the isOn on the toggle, the value was correct but the pressed color that I specified was not showing. I had to call the Select() on the Toggle component to make sure that next to being 'isOn' it also showed as a selected toggle color.
Answer by $$anonymous$$ · Feb 20, 2017 at 10:19 AM
yes, it is possible. use bool var for every image-effects so every time when open the game set-get player pref for image-effects.
$$anonymous$$ay I know what's the syntax for changing the Toggle UI?
Fore example, I have a Toggle variable: $$anonymous$$otionBlurToggle.
Is there something like: $$anonymous$$otionBlurtoggle.checked = true;
I am not exactly sure what you are asking for, but I hope this answers your question:
// This will show you the state of your toggle in the console
bool toggleState;
$$anonymous$$otionBlurtoggle.isOn = toggleState;
Debug.Log(toggleState);
// This will toggle your Button on and off
$$anonymous$$otionBlurtoggle.isOn = !$$anonymous$$otionBlurtoggle.isOn
// Simply put: Turn my Toggle-State into what it is not
// So if on, turn it off. If off, turn it on
Answer by codemaker2015 · Sep 01, 2021 at 03:30 PM
You can use isOn
attribute to check whether the toggle button is checked or not. Also, set the isOn attribute to toggle the button.
using UnityEngine;
using UnityEngine.UI;
public class ToggleDemo : MonoBehaviour
{
[SerializeField]
private Toggle toggle;
private void Start()
{
Debug.Log("Toggle button status: " + toggle.isOn);
}
public void ChangeToggleTrue()
{
toggle.isOn = true;
Debug.Log("Toggle button status: " + toggle.isOn);
}
public void ChangeToggleFalse()
{
toggle.isOn = false;
Debug.Log("Toggle button status: " + toggle.isOn);
}
}
Answer by Must_I_have_a_name · Apr 09 at 10:53 PM
There is a place where you should not change the .isOn value. That place is in a function which is triggered by a user clicking on the toggle. Do not monkey around the associated .isOn value THERE. There's a race condition. Instead put a check in Update to see if you should change isOn for a toggle. If you try to change the isOn of a toggle in a function caused by a user click it may or may not work
Your answer
![](https://koobas.hobune.stream/wayback/20220612111431im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Is there a better way to access the single active Toggle in a ToggleGroup? 4 Answers
Toggling a game objects active state 1 Answer
Unlockable Levels 3 Answers
Slider won't slide, issue assigning PlayerPrefs and then changing the PlayerPrefs' value 1 Answer
Why are my UI images appearing in front of my 3D objects 2 Answers