Search Unity

ToggleGroup and events

Discussion in 'UGUI & TextMesh Pro' started by GarethIW, Aug 22, 2014.

  1. GarethIW

    GarethIW

    Joined:
    Jun 24, 2014
    Posts:
    50
    I'm probably missing something simple here.

    You can add an event to a single Toggle control. The ToggleEvent passes a bool to your receiving method, but nothing more. This is fine for a single toggle that you just want to react to when it's toggled on and off.

    When you have a toggle group, especially one into which you have instantiated the items yourself, it would be useful to be able to have one delegate to receive the onchange event for all of the Toggles in the group. We can't use ToggleEvent for this as it doesn't include a reference to the Toggle gameobject.

    Is there currently a way to do this?
     
  2. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    When you create your toggle / group or add the callback you can do this with a delegate as you have access to the toggle and the group at that stage. The example below is for button, but the same ideas can be applied to toggle.

    Code (csharp):
    1.  
    2. button.onClick.AddListener (delegate { Debug.Log("The Button is " + button.name); } );
    3.