Based on this question on the Unity forum, I've made this code to instantiate a button and add an onClick
event.
// Setting some other component properties right of the prefab.
shopTurretItem.GetComponent<UnityEngine.UI.Button>().onClick.AddListener(() => {
UnityEngine.Debug.Log("Click");
});
Instantiate(shopTurretItem, transform);
The variabele shopTurretItem
is my prefab type of UnityEngine.GameObject
.
The problem is that the log Click
will never be shown. If I add breakpoints on the first code line, the compiler will stop the code. If I place it into the closure, it'll never be hit if I click on that button.
This is the inspector of my prefab shopTurretItem
.
What's wrong with this code?