Search Unity

onclick.addlistener not working ?

Discussion in 'Scripting' started by zyonneo, Aug 21, 2019.

  1. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    Code (CSharp):
    1.  
    2.    public void Load()
    3.     {
    4.         loadpanel.SetActive(true);
    5.         string mainpath = Application.persistentDataPath;
    6.         DirectoryInfo dir = new DirectoryInfo(mainpath);
    7.  
    8.         infos = dir.GetFileSystemInfos("*.json").OrderBy(i => i.CreationTime).ToArray();
    9.  
    10.         for (int i = 0; i < infos.Length; i++)
    11.         {
    12.           //listLoaditems contains the instatiated prefab containing two text and a button which is the third child.
    13.          var delbutton = listLoaditems[i].transform.GetChild(2).GetComponent<Button>();
    14.            delbutton.onClick.AddListener(() => LoadDelete(i));
    15.         Debug.Log("del name = " + delbutton.name);//Here I am getting correct button name.
    16.         }
    17.    }
    18.    public void LoadDelete(int index)
    19.     {
    20.         Debug.Log("inndex value = " + index);
    21.         Destroy(listLoaditems[index]);
    22.         listLoaditems.RemoveAt(index);
    23.  
    24.        
    25.     }
    26.  
    27.  
    I cannot add the onclick listener to the button instantiated.The button is getting detected I believe,I am trying to print button name and the same name for the button in the hierarchy is getting printed.It does not call the LoadDelete function.
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Can't tell what's wrong with button, code looks ok, if only are you sure there's any button component on child indexed 2? You also have error with your closure. The anonymous function will remember reference to i vairable. The loop will comlete. i will become = infos.Length, after that then handler will be called, the index will be infos.Length all the times.
     
    SparrowGS likes this.
  3. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    Code (CSharp):
    1. int index = i;
    2. delbutton.onClick.AddListener(() => LoadDelete(index) );
    3.  
    4.  
    I changed it like this.Still not working.
    I manually added a method
    Code (CSharp):
    1. public void LoadDelete()
    2.     {
    3.         Debug.Log("Drag n drop method");
    4.      
    5.     }
    I took the prefab and dropped in hierarchy,then selected the button dragged and dropped the component containing the script containing LoadDelete() method and clicked the prefab and gave Applyall .For some reason it is not getting saved
     
  4. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    The button Onclick in the inspector is always empty even though I click Applyall in the prefab
     
  5. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    In the ispector only persistent listeners are shown. You won't see those added in runtime I think.
     
    SparrowGS likes this.
  6. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    Yes I understand that..the one added by dragging the script does show in the button ...but once I make it a prefab and instantiate it the button Onclick goes missing...
     
  7. zyonneo

    zyonneo

    Joined:
    Apr 13, 2018
    Posts:
    386
    https://codeshare.io/5QYzdm

    Also hit game object name is given as below

    Hit Gameobject = TopOutRight

    only one button inside the prefab
     
    Last edited: Aug 22, 2019
    kevinbeltranx likes this.