Search Unity

Add a Toggle OnValueChanged Event using script

Discussion in 'UGUI & TextMesh Pro' started by Aurecon_Unity, Apr 28, 2015.

  1. Aurecon_Unity

    Aurecon_Unity

    Joined:
    Jul 6, 2011
    Posts:
    241
    Hi All

    I'd like to add an On Value Changed event through an editor script - my toggles are instantiated from a prefab but I need to add an On Value Changed event to them after they are instantiated.

    I can't seem to find this info anywhere - I just need the script equivalent of dragging a gameobject onto the Object slot and selecting the relevant public function.

    Thanks!
     
  2. Ramcat

    Ramcat

    Joined:
    Aug 16, 2014
    Posts:
    95
    I'm not sure there is such a thing, if there is I would love to know it too. But to add a listener to a toggle's onValueChanged would look like this:
    ToggleVar.onValueChanged.AddListener((t) => { MethodName(); });

    Should that method return a Boolean? I don't know. Where is the documentation for this sort of thing? I don't think there is any with code examples.
     
  3. Aurecon_Unity

    Aurecon_Unity

    Joined:
    Jul 6, 2011
    Posts:
    241
    Yeah I have gotten the functionality I need through the .AddListener function, but it's all invisible - I wish we could somehow add a new entry into the event list so it's visible!
     
  4. Ramcat

    Ramcat

    Joined:
    Aug 16, 2014
    Posts:
    95
  5. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689