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

Nurullah picture Nurullah · Jul 26, 2015 · Viewed 22.6k times · Source

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);

    }
}

Answer

Uri Popov picture Uri Popov · Jul 26, 2015

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
    }

}