Change Onclick Object via script

Is there a way to change the object via script?
I’ve got 4 Npcs that they each have the same scripts in but different parent.
right now I’m dragging the npc from editor but because of that the button always knows only one Npc and the other gets index error (trying to add object to list).
any way to switch it depending on raycast? or other ways?
thank you.


As a matter of fact there is! You can add listeners in code by using Button.onClick.AddListener().
You can find out more here: Unity - Scripting API: UI.Button.onClick