How do you use the addListener call on a toggle?

I had a hard drive crash and lost all my work over the last 3+ months, as i am going back and attempting to recreate it I can’t seem to recall how you add a listener correctly to a toggle so that you can make something happen differently if the toggle is ticked.

    shrimpFishingToggle.onValueChanged.AddListener();

its not liking that saying that there is 0 arguments needed for addListener, so when i take it out and try to just use the ; its not liking that either. Could someone let me know what i am not remembering correctly here? thanks

You need a parameter for AddListener: a delegate with no return type and no arguments. Like this:

class MyClass : MonoBehaviour
{
  void Start()
  {
    shrimpFishingToggle.onValueChanged.AddListener(OnShrimpClicked);
  }

  void OnShrimpClicked()
  {
    // Do stuff...
  }
}
1 Like

Thats how it was … thanks! I remember now!

You can also add anomonous functions using lambda syntax. Its useful if you need to add the same functionality to a bunch of different buttons in a loop.

1 Like

thanks i will look that up. i haven’t heard of lambda syntax before.