Author Topic: Dynamic UITable - almost have it, just cant change text  (Read 3447 times)

patm

  • Guest
Dynamic UITable - almost have it, just cant change text
« on: March 26, 2013, 07:15:25 AM »
I've been messing with UITable for a couple of days and finally have it adding entries dynamically and displaying in all the right places etc. The only problem I have left now is figuring out how to change the text of a UILabel child.

I cheated and used a UITable demo child (Quest 1) and renamed the UILabel to simply "label". If I just instantiate with NGuiTools I get the proper number of child rows on the table. If I try to change the child text I only get one entry with its original text and no errors reported. THis is the code I'm using

listEntry = NGUITools.AddChild(tableObject, entryPrefab);
listEntry.GetComponent("label").text="Yeahbaby!";

Any clues? It's probably simple as pie by my brain is frazzled!

THanks!

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Dynamic UITable - almost have it, just cant change text
« Reply #1 on: March 26, 2013, 08:49:25 AM »
Make a helper script on your list entry and get component on that. Inside it, you can have a proper reference to the label you need.

I assume your list entry is a prefab that you instantiate.

patm

  • Guest
Re: Dynamic UITable - almost have it, just cant change text
« Reply #2 on: March 26, 2013, 01:15:10 PM »
Now that I've been away for a few hours and doing completely unrelated stuff I realized my mistake. I got the gameobject that holds "label" then got the object for "label" itself and try to change the text on that. However, that's a transform, not the script attached to the transform. When I get home (at work now) I'll figure out how to talk to the script attached to "label".

Might even be just

listEntry.GetComponent("label").GetComponent("UILabel").text="Yeahbaby!";

Though I think I remember something about using another command to retrieve script references.

patm

  • Guest
Re: Dynamic UITable - almost have it, just cant change text
« Reply #3 on: March 26, 2013, 09:43:08 PM »
Now that I'm home I got it working (for the most part). Here's the steps I used (the table and prefab objects were dragged into the inspector).

  1. listEntry = NGUITools.AddChild(tableObject, entryPrefab);
  2. labelObject = listEntry.Find("LabelTitle");
  3. labelObject.GetComponent(UILabel).text=newServer.name;
  4.  

Now I have to figure out why newServer.name only gets assigned to the first instance...

patm

  • Guest
Re: Dynamic UITable - almost have it, just cant change text
« Reply #4 on: March 27, 2013, 06:28:14 PM »
For completeness for the next newb like me trying to use the UITable dynamically. In my case I'm trying to make a table of game servers to choose from. I'm using the example "Quest" object & children as a prefab and just changing the text in the main label and the tween label. I changed the names from "Label - Title" and "Label - Text" to LabelTitle and LabelText respectively.

create an instance of the table entry object with NGUITools

listEntry = NGUITools.AddChild(tableObject, entryPrefab);

Then access the label with:

listEntry.transform.FindChild("LabelTitle").GetComponent( UILabel).text = newServer.name;

The tween label is a bit more involved:

listEntry.transform.FindChild("Tween").FindChild("LabelText").GetComponent(UILabel).text = newServer.IP;