Search Unity

How to connect Listitems to a bool

Discussion in 'Scripting' started by The_Arrival, Jan 28, 2015.

  1. The_Arrival

    The_Arrival

    Joined:
    Dec 3, 2014
    Posts:
    82
    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 the NGUI-Plugin, but i guess my question is a general one.

    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:

    Code (CSharp):
    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 again. Is something like this even possible?

    Another question which is kinda NGUI-specific, but maybe someone here has an answer:

    Instead of adding the available labels in the inspector i would like to add them automaticaly. I tried something like this:

    Code (CSharp):
    1.         uiRoot = GameObject.Find("UI DebugLabels");
    2.  
    3.         for (int i = 0; i < uiRoot.transform.childCount; i++)
    4.         {
    5.             Transform temp = this.uiRoot.transform.GetChild(i);
    6.             UILabel tempLabel = temp as UILabel;
    7.         }
    But i couldn´t find a way to typecast my transform into UILabel. Does anyone knows a better way?
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    You could either enable/disable the UILabel component depending on if it is assigned to a unit (only applicable if they label is empty when not assigned) or you can create a class that contains both the UILabel and a boolean and expose that instead.
    Code (csharp):
    1. public List<LabelContainer> enemyLabels;
    2.  
    3. [System.Serializable]
    4. public class LabelContainer {
    5.     public UILabel label;
    6.     public bool isActive;
    7. }
    Code (csharp):
    1. temp.GetComponent<UILabel> ()
     
    The_Arrival likes this.
  3. The_Arrival

    The_Arrival

    Joined:
    Dec 3, 2014
    Posts:
    82
    I´ve choosen the custom class solution!

    So quick and easy. Thanks for the help. This works totally fine. This is such a good idea to make Lists more flexible.

    I also posted the typecast-question on the NGUI forums so i will post the answer here, if i get one to make this thread viable for other lookups