unity how to add listener to a button from script code example
Example: unity onclick addlistener
public int index;
private Button myselfButton;
void Start()
{
myselfButton = GetComponent<Button>();
myselfButton.onClick.AddListener(() => actionToMaterial(index));
}
void actionToMaterial(int idx)
{
Debug.Log("change material to HIT on material : " + idx);
}
void Destroy()
{
myselfButton.onClick.RemoveListener(() => actionToMaterial(index));
}