1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

AddListener will not listen to onClick event

Discussion in 'Programming/Internet' started by H. Pauwelyn, Sep 16, 2020.

  1. H. Pauwelyn

    H. Pauwelyn Guest

    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(() => {

    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?

    Login To add answer/comment

Share This Page