Author Topic: I really don't know why NGUI EventDelegate.Parameter does not work.  (Read 3313 times)

joonyboy

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
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.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: I really don't know why NGUI EventDelegate.Parameter does not work.
« Reply #1 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. });