Edit Prefab button event via script.

Hello,

I tried using a button for my game as prefab. Importing it and placing it wasn’t a very big problem, but when you Instantiate the button it has no function which will be called. So now I’m asking myself how I am able to set up the Button function outside of the Editor by script?

Hope you can help and understand me.

Greetings
UndeadLeech

Assuming you have a gameObject that has a Button component on it, you can do this (in C#):

Button buttonComponent = gameObject.GetComponent();
buttonComponent.onClick.AddListener(yourClickHandlerFunction);

can do it with an anonymous click handler too,

buttonComponent.onClick.AddListener(() =>
{
_fsm.State = new ActionInputState();
});