- Home /
How to add OnClick listener in script to see it in inspector?
Hi! I've got a bit problem with OnClick Listeners. I add a listener to a UI Button and it works but I can not see it in the inspector.
go.GetComponent<Button>().OnClick.AddListener( () => .... )
I'm making some tool that manipulates gameobjects in design time and it should add onclick events and save them to a prefab but it doesn't.
Answer by darkStar27 · Mar 26, 2019 at 09:39 AM
For anyone who is searching for the same problem, use this:
UnityEventTools.AddPersistentListener(unityEvent, fuctionToCall);
You can refer to this link to know more about AddPersistentListener.
There are many more functions to choose from, refer here.
WARNING
THIS ONLY WORKS IN THE EDITOR, YOU WON'T BE ABLE TO BUILD YOUR GA$$anonymous$$E IF YOU DON'T REPLACE IT WITH THE CO$$anonymous$$$$anonymous$$ON WAY TO ADD LISTENERS.
This works if used for automated scene setup running in the editor and runs in build perfectly.
This will not work if UnityEventTools is called from runtime