Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problem with CreateButton.GetComponent<Button>().onClick.AddListener(() => { Dialog(); });

Discussion in 'Scripting' started by Drockk, Aug 10, 2015.

  1. Drockk

    Drockk

    Joined:
    Aug 10, 2015
    Posts:
    4
    Hello, I have a problem with :
    CreateButton.GetComponent<Button>().onClick.AddListener(() => { Dialog(); });
    Does't add function to a button.
    Code (csharp):
    1.  
    2.     public void CreateDialogButton(string ButtonText, int id, DialogTopicVoid Dialog)
    3.     {
    4.  
    5.         CreateButton = Instantiate(DialogButton);
    6.         CreateButton.transform.parent = DialogWindowPanel.transform;
    7.         if( id == 0 )
    8.         {
    9.  
    10.             PosY = 85;
    11.  
    12.         }
    13.         else
    14.         {
    15.  
    16.             PosY = PosY - 30;
    17.  
    18.         }
    19.         CreateButton.GetComponent<RectTransform>().localPosition = new Vector3( PosX, PosY, PosZ );
    20.         CreateButton.GetComponent<RectTransform>().sizeDelta = new Vector2( SizeX, SizeY );
    21.          CreateButton.GetComponentInChildren<Text>().text = ButtonText;
    22.         CreateButton.name = ButtonText;
    23.         CreateButton.GetComponent<Button>().onClick.AddListener(() => { Dialog(); });
    24.      
    25.     }
    26.  
     
    Last edited: Aug 10, 2015
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Code tags please.

    What does happen when you click the button created by this code?

    Note that runtime listeners will not show up in the inspector.
     
  3. Drockk

    Drockk

    Joined:
    Aug 10, 2015
    Posts:
    4
    Nothing happens
     
  4. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,510
    I had trouble assigning onClick listeners within a loop, and found that separating the assignment of the listener fixed my issue. I'm not familiar with 'DialogTopicVoid' and assigning methods this way, but what if you did something like:
    Code (CSharp):
    1. public void CreateDialogButton(string ButtonText, int id, DialogTopicVoid Dialog) {
    2.     CreateButton = Instantiate(DialogButton);
    3.     CreateButton.transform.parent = DialogWindowPanel.transform;
    4.    
    5.     PosY = (id == 0) ? 85 : PosY - 30;
    6.  
    7.     CreateButton.GetComponent<RectTransform>().localPosition = new Vector3( PosX, PosY, PosZ );
    8.     CreateButton.GetComponent<RectTransform>().sizeDelta = new Vector2( SizeX, SizeY );
    9.     CreateButton.GetComponentInChildren<Text>().text = ButtonText;
    10.     CreateButton.name = ButtonText;
    11.  
    12.     AssignDialog(CreateButton.GetComponent<Button>(), Dialog());
    13. }
    14.  
    15. public AssignDialog(Button btn, DialogTopicVoid dialog) {
    16.     btn.onClick.AddListener(() => dialog);
    17. }
     
  5. Drockk

    Drockk

    Joined:
    Aug 10, 2015
    Posts:
    4
    Doesn't works :(
    DialogTopicVoid is a delegate void because the function is transmitted from another script but if I try use function from this scripts it's the same.
     
  6. Drockk

    Drockk

    Joined:
    Aug 10, 2015
    Posts:
    4
    Someone help ?
     
  7. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    So, I've tried all the methods suggested by you guys, and all of them works. So, I think your problem lies elsewhere. Is your scene correctly set up? Is there an EventSystem in your scene? If there isn't there won't be any click events generated.