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.
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;
}
}
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