Author Topic: Using Containers (aka Lists/Arrays/Dictionaries) to Populate Labels  (Read 4709 times)

Aneurysm

  • Guest
Hi ArenMook,

Firstly thank you very much for developing such a nice (and easy) UI solution.

EDIT: I am clearly just retarded as I have subsequently discovered that a plain UILabel.text works perfectly fine. I could have sworn I tried it earlier only for it to not succeed, still, problem solved.


I am having a little bit of difficulty trying to work my way around creating GUI lists (think for example of a resource list in an RTS). I have been trying to create tables of prefabs comprised of labels that then get populated with the relevant information stored in corresponding containers. I am however hitting a wall because unity will not allow me to access the .mText component of the UILabel in order to change its value; it instead gives me an error message

"'UILabel.mText' is inaccessible due to its protection level."

My question then is:

a. Am I trying to do this in a round about and idiotic way that would be solved with a different approach.

b. If this is the best approach, how do I bypass this (I assume Serialization) issue that prevents me from changing the text inside of a UILabel?

Below is my Unityscript code that is iterating through my Dictionary in order to populate the Table.

  1. #pragma strict
  2.  
  3. import System.Collections.Generic;
  4.  
  5. var lineInputPrefab : GameObject;
  6. var parentObject : GameObject;
  7.  
  8. public enum ResourceType
  9. {
  10. coal = 0,
  11. oil = 1,
  12. wood = 2,
  13. gold = 3,
  14. wine = 4
  15. }
  16.  
  17. static public var resourceDictionary = new Dictionary.<ResourceType,int>();
  18.  
  19. function Start ()
  20. {
  21.         var parentObject = GameObject.FindWithTag("resList").transform;
  22.  
  23.         resourceDictionary[ResourceType.coal] = 100;
  24.         resourceDictionary[ResourceType.wood] = 200;
  25.         resourceDictionary[ResourceType.wine] = 50;
  26.         resourceDictionary[ResourceType.oil] = 465;
  27.         resourceDictionary[ResourceType.gold] = 10;
  28.        
  29.         for(var value in resourceDictionary)
  30.         {
  31.                 print(value);
  32.         }
  33. }
  34.  
  35. function PopulateList ()
  36. {
  37.         //This is currently instantiating a number of lines equal to the size of the Dictionary
  38.         for(var value in resourceDictionary)
  39.         {
  40.                 Instantiate(lineInputPrefab, transform.position, transform.rotation);
  41.                 print("list successfully populated");
  42.         }
  43.        
  44.         var newInput = GameObject.FindGameObjectsWithTag("resInput");
  45.        
  46.         //This is moving the newly created objects ("lines") to be parented under the PlayerResourceTable object.
  47.         for (var value : GameObject in newInput)
  48.         {
  49.     value.transform.parent = parentObject.transform;
  50.         var x = 0;
  51.         value.SendMessage("AddValues", resourceDictionary[x++]);
  52.         }
  53.         //This SndMsgUpWards is just re-positioning the newly created objects so that they line up.
  54.         parentObject.SendMessageUpwards("RefreshList");
  55. }

The function that the "SendMessage("AddValues", resourceDictionary[x++]);" line is calling is below:

  1. #pragma strict
  2.  
  3. function AddValues(dictText : String)
  4. {
  5.         var newText : UILabel = gameObject.GetComponent(UILabel);
  6.     newText.mText = dictText;
  7. }

It is this second script that is causing the error message.

Any help would be greatly appreciated, also to anyone responding, please keep in mind that while I am not an idiot, I am an artist/designer crossing over into programming territory, so keep it simple for me ok :P
« Last Edit: April 23, 2012, 12:49:33 PM by Aneurysm »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Using Containers (aka Lists/Arrays/Dictionaries) to Populate Labels
« Reply #1 on: April 23, 2012, 01:46:27 PM »
I kept getting these "how do I set the text of a label?" questions roughly once a day, which is why I added it to the FAQ:

http://www.tasharen.com/forum/index.php?topic=6.0 :)

Aneurysm

  • Guest
Re: Using Containers (aka Lists/Arrays/Dictionaries) to Populate Labels
« Reply #2 on: April 23, 2012, 06:29:49 PM »
I kept getting these "how do I set the text of a label?" questions roughly once a day, which is why I added it to the FAQ:

http://www.tasharen.com/forum/index.php?topic=6.0 :)

My apologies, I made sure I did a search before posting...and checked the FAQ... I guess I just wasn't doing anything right last night :(