Search code examples
c#unity-game-enginetoggleoncheckedchanged

How to use Unity Toggle Check Function (checked and unchecked)


I'm trying to toggle. When I click on the toggle (if it is on I mean checked), it closes some gameobject, if I click on the toggle (if it is off I mean unchecked) it opens some gameobject but I don't know which function unity toggle.onselect? toggle.onValuesChanged? or others which one

public GameObject controlledObject;
public NamesGet nameController;
public Text text;
public Button button;
public Toggle toggle;
private bool deneme;
public void FixedUpdate()
{
    text.text = controlledObject.name;

    if(?????????)
    {
        controlledObject.SetActive(!controlledObject.activeSelf);

    }
}

Solution

  • I would use something like this :

    toggle.onValueChanged.AddListener((value) =>
        {
            MyListener(value);
       });//Do this in Start() for example
    
    public void MyListener(bool value)
    {
     if(value)
        {
        //do the stuff when the toggle is on
        }else {
        //do the stuff when the toggle is off
        }
    
    }