Author Topic: GetComponent Script Question  (Read 3250 times)

cardician

  • Guest
GetComponent Script Question
« on: May 27, 2013, 12:12:20 PM »
In my GUI I'm making use of an element that is like the Quest Log example. Only the elements under the Table are dynamically generated from a prefab so that all I have to do is implement an instance and change the two labels. I'm having trouble figuring out how to access the description label though. Here is what I've done so far, feel free to point out if I'm going in an entirely wrong direction.

Table
- GameObject
- - Name Label
- - Sprite
- - GameObject (with tween scripts)
- - - Description Label

I added a script to the Table element of the Quest log. Then using NGUITools.AddChild, I add an instance of a prefab which is the rest of the elements. So a label, a spliced image, an empty GameObject with the two tweener scripts, and the description label attached to that empty object. All that works well and I can even access the first label using GetComponentInChildren<UILabel>. However, as I mentioned, I can't figure out how to access the label underneath the empty gameobject. I tried GetComponentsInChildren<UILabel> but that only returns the top label, not the one underneath the GameObject. I'm assuming I have to first get a handle of some sort on the empty GameObject and then I'll be able to access the label underneath it. Any suggestions on how I should do this? Or if I'm going about this wrong, any corrections on the proper way to implement it? Thanks very much.

----Update----
I've continued playing with it and I've realized that I believe the problem lies with the fact that the empty Gameobject with the Tween scripts and the description under it are both disabled initially. I was playing with the SetActiveChildren method and this allowed me to access the Tween and Description Label. I suppose I could leave them initially enabled in the prefab so that I can modify their texts and then disable them. Is there a better way to do this though?
« Last Edit: May 27, 2013, 12:45:13 PM by cardician »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: GetComponent Script Question
« Reply #1 on: May 28, 2013, 02:06:08 AM »
Create a script with a public variable of type UILabel, attach it to your GameObject, and in inspector drag & drop the label into this field. There you go, now you can GetComponent of your script type, and access its public field directly. This is common practice when you need to have quick access to values. Add other public fields there as well -- one for the description, one for the sprite, one for the tween, etc.

cardician

  • Guest
Re: GetComponent Script Question
« Reply #2 on: May 28, 2013, 10:57:03 AM »
Makes perfect sense and works much better than the workaround I came up with. My Unity inexperience is clearly showing. Thank you very much for taking the time to answer that, much appreciated.