Hello, Im making a procedually generating list using of course Javascript and Unity's UI (Thank you so much unity!). Right now for me its a bit of a hassle to figure it out because no one seems to provide a clear answer for me so Id thought Id ask you guys. Right now I'm currently trying to add an Listener event so when I click on my button it will call my function in the script, however its not like in the old days of unity now especially since you have to add a listener event for the UI button. Heres my line of code right now. Code (JavaScript): caseButt.onClick.AddListener( caseButtons[i].GetComponent(ListButton).OnClick() ); I had a look at the scripting reference but they never gave an example script of how its done. While adding the first parenthesis after AddListener, monoDevelop popped an yellow box saying - it also says in a little grey box saying I have 1 out of two, I can see that I only have function but my question is, What do I use for the call?
Im not that good with JS but I'll assume it's similar to C#. You want to call a function when a user clicks a button right? Are you using the new UI system to create the button?
Try this Code (csharp): caseButt.onClick.AddListener( function() { caseButtons[i].GetComponent(ListButton).OnClick() } );
Or this: Code (csharp): caseButt.onClick.AddListener( caseButtons[i].GetComponent(ListButton).OnClick );
Thanks @blizzy! I was wondering what was I missing for a minute until i realized you had no () after onClick.
You need to pass a function reference to AddListener(), but in your original code, you were calling your function and passing its result instead.