Author Topic: Dynamically Generated Buttons - UIEventListener (&Passing Args)  (Read 2801 times)

Vaei

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
Dynamically Generated Buttons - UIEventListener (&Passing Args)
« on: January 20, 2014, 07:25:07 AM »
Hi there,

I have a loop that generates a button for each instance that I have connected to my server. It's a join button.

What is the proper way to handle this?

I can get the button by:

  1. GameObject button = listing.transform.FindChild("Connect_Button").gameObject;

But of course it wont be stored in an array or anything, which I could do too.

Then I can initialize the UIEventHandler BUT I don't know if I can do this in the loop, or if it has to be in start, or what. AND I need to pass in an argument (the id of the instance) which I'm not sure how to do because they don't take arguments.

Please help :) Thank you.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Dynamically Generated Buttons - UIEventListener (&Passing Args)
« Reply #1 on: January 20, 2014, 07:34:36 AM »
My suggest... never use Find. Ever.

Also, never try to find something on children. Instead have children look up scripts on parents.

Reason being, a parent can exist without a child, but a child cannot exist without a parent.

So... have a script on your children that will either forward events to an appropriate script on the parent (NGUITools.FindInParents is handy!), or call the correct function directly.

Vaei

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: Dynamically Generated Buttons - UIEventListener (&Passing Args)
« Reply #2 on: January 20, 2014, 08:04:49 AM »
Ah.. of course! Thank you