Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: joonyboy on February 20, 2017, 04:13:27 AM

Title: I really don't know why NGUI EventDelegate.Parameter does not work.
Post by: joonyboy on February 20, 2017, 04:13:27 AM
Last time there was no problem to use EventDelegate

but today I don't know why  EventDelegate.parameters[0] returns null reference exception.

The code with problem is this.

void OnEnable() {
        removeAllList();
        Group[] searchedGroups = controller.groupStore.searchedGroups;
        int length = searchedGroups.Length;
        if(length == 0) {
            emptyMessage.SetActive(true);
            return;
        }

        for(int i=0; i<length; i++) {
            GameObject item = Instantiate(container);

            item.transform.SetParent(grid.transform);
            item.transform.localPosition = Vector3.zero;
            item.transform.localScale = Vector3.one;

            item.GetComponent<GroupIndex>().id = searchedGroups.id;
            item.transform.Find("LocationLabel").GetComponent<UILabel>().text = searchedGroups.locationDistrict + " " + searchedGroups.locationCity;
            item.transform.Find("GroupNameLabel").GetComponent<UILabel>().text = searchedGroups.name;
            item.transform.Find("MemberCountLabel").GetComponent<UILabel>().text = "멤버 " + searchedGroups.membersCount + "명";

            EventDelegate addEvent = new EventDelegate(this, "addEvent_test");

            EventDelegate.Parameter param = new EventDelegate.Parameter();
            param.obj = item;
            param.field = "Index";
            addEvent.parameters[0] = param;

            GameObject tmp = item.transform.Find("GotoDetailBtn").gameObject;

            EventDelegate.Add(tmp.GetComponent<UIButton>().onClick, addEvent);
        }
    }

    public void addEvent_test() {

    }

please anyone could help me. thanks.
Title: Re: I really don't know why NGUI EventDelegate.Parameter does not work.
Post by: ArenMook on February 21, 2017, 12:41:36 PM
That's a very ugly way of working with delegates, but I'd say check your function parameters. Does it even have an expected parameter?

I'd do it using an anonymous delegate. Way cleaner. No need to pass anything.
  1. var button = tmp.GetComponent<UIButton>();
  2.  
  3. EventDelegate.Add(button.onClick, delegate ()
  4. {
  5.     Debug.Log("Clicked on item " + item.name, item);
  6. });