Author Topic: Question about UI Button Label  (Read 6876 times)

Dennis59

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
Question about UI Button Label
« on: March 28, 2013, 08:07:34 AM »
I am dynamically creating a new UIButton for each available server in a UIGrid using the following code.  After creating the button I want to add the name of the server as the text on the button.

  1.         void Start ()
  2.         {
  3.                 GameObject grid = GameObject.Find("ChildGrid");
  4.                 List<ServerList.Entry> list = TNLobbyClient.knownServers.list;
  5.                
  6.                 for(int i = 0 ; i < list.size ; i++)
  7.                 {
  8.                         //instantiate a new button and give it the server name
  9.                         GameObject newBtn = NGUITools.AddChild(grid, btn);
  10.                         grid.GetComponent<UIGrid>().Reposition ();
  11.  
  12.                         //add server name to button
  13.  

It would seem that there should be a way to access the label directly since the button object is known.  I've tried UILabel testObj = newBtn.GetComponent<UILabel>(); but that gives an error.

Is there a way to access this directly or is it necessary to use find?

broknecho

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 33
    • View Profile
Re: Question about UI Button Label
« Reply #1 on: March 28, 2013, 10:12:16 AM »
you should use:

  1. newBtn.GetComponentInChildren<UILabel>();

That should get you the UILabel under the button.

Dennis59

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
Re: Question about UI Button Label
« Reply #2 on: March 29, 2013, 08:50:45 AM »
broknecho, thank you for the response.  I'll give it a try!