Author Topic: connecting a bool to UILabel-List and typecast to UILabel  (Read 1282 times)

The-Arrival

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 29
    • View Profile
connecting a bool to UILabel-List and typecast to UILabel
« on: January 28, 2015, 03:12:53 PM »
Hello Community
i´m looking for a way to create a List/Array of a class connected to a bool variable to keep track if it´s used or not.
I´m working with NGUI v3.7.9
So i´m having a Debug-UI running were i can track values of every unit in game. Since i´m implemented a spawner for them i have to find a way to attach a not-used UI-Label to a spawned enemy. When he dies the label can be used by another one.
In the inspector i´m adding all available Labels in this list:
  1. public List<UILabel> enemyLabels;
Now i would like to connect a bool to every entry set to false. When the label is assigned change it to true, when the unit dies, set it back to false again and make the label available. Is something like this even possible?

The other question is this:
Instead of adding the available labels in the inspector i would like to add them automaticaly. I tried something like this:
  1.         uiRoot = GameObject.Find("UI DebugLabels");
  2.         for (int i = 0; i < uiRoot.transform.childCount; i++)
  3.         {
  4.             Transform temp = this.uiRoot.transform.GetChild(i);
  5.             UILabel tempLabel = temp as UILabel;
  6.         }
But i couldn´t find a way to typecast my transform into UILabel. Does anyone knows a better way?

The-Arrival

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: connecting a bool to UILabel-List and typecast to UILabel
« Reply #1 on: January 28, 2015, 03:54:01 PM »
Update: The Listquestion got solved on the Unity-Forums using this solution:

  1.    public List<enemyLabel> enemyLabels;
  2.  
  3.     [System.Serializable]
  4.     public class enemyLabel
  5.     {
  6.         public UILabel label;
  7.         public bool isUsed = false;
  8.     }

So i´m still struggeling on the NGUI specific typecast issue, or another way to auto-add my labels to my List or maybe better create a new lable on runtime, if i run out of premade labels.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: connecting a bool to UILabel-List and typecast to UILabel
« Reply #2 on: January 29, 2015, 12:54:10 PM »
None of this is NGUI-related. It's a very basic scripting question. Adding things to a custom list is a trivial matter. Create a new instance of your enemyLabel, set its label and isUsed properties, then add this instance to the list. Like I said... very beginner programming question and nothing to do with NGUI.

The-Arrival

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: connecting a bool to UILabel-List and typecast to UILabel
« Reply #3 on: January 29, 2015, 03:05:38 PM »
Yeah, got it! I´m a beginner. Thx for this information. I asked also here about this, cause i thought there is maybe a build-in solution for something like this i didn´t recognised.

What about the second question about typecasting? Also too newbie-ish to get an answer here?

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: connecting a bool to UILabel-List and typecast to UILabel
« Reply #4 on: January 29, 2015, 05:30:09 PM »
You don't cast a transform to get a different component, you use GetComponent<Type>. Like so:
  1. UILabel label = temp.GetComponent<UILabel>();

The-Arrival

  • Newbie
  • *
  • Thank You
  • -Given: 5
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: connecting a bool to UILabel-List and typecast to UILabel
« Reply #5 on: January 30, 2015, 06:17:58 AM »
Thanks for the polite answer. Components and Objects are still confusing me from time to time.