1

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

    }
}
Alex
  • 9,891
  • 11
  • 53
  • 87
Nurullah
  • 103
  • 3
  • 4
  • 14

2 Answers2

6

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
    }

}
Uri Popov
  • 2,127
  • 2
  • 25
  • 43
  • if i use your code , unity send me to this error : The best overloaded method match for 'UnityEngine.Events.UnityEvent.AddListener(UnityEngine.Events.UnityAction)' has some invalid arguments i write this code: toggle.onValueChanged.AddListener(MyListener); – Nurullah Jul 26 '15 at 11:48
  • @Nurullah I changed my answer so that it works. I tested it on my end. – Uri Popov Jul 26 '15 at 12:41
0

New instead using OnValueChanged why not use EventTrigger - Pointer Click so it will only be called Once. same as adding Listener via script but its more quick and handy.

Jex Belt
  • 1
  • 1