Search Unity

GUI array / GameObject array problem

Discussion in 'Scripting' started by wilhelmscream, Nov 28, 2015.

  1. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    I'm having a slight issue with a GUI question.

    My objective : To have a script do the following.....

    - Count the number of children with a given tag
    - Read a unique variable on each of the above
    - create an array containing said gameObjects
    - Generate a GUI box containing a unique, individual label for each gameObject in the array.

    So far I'm able to accomplish the first three things on the list, however.....

    My code so far:

    Code (JavaScript):
    1. var companyName : String;
    2. var showSubunits = false;
    3. var unitList : int;
    4. var subUnits : GameObject [];
    5.  
    6. function Start () {
    7. }
    8.  
    9. function OnGUI () {
    10.         if (showSubunits) {
    11.             GUI.BeginGroup (Rect (80, 563 - unitList, 100, unitList));
    12.                 GUI.Box (Rect (0, 0, 100, unitList), "");
    13.                  for (var company : GameObject in subUnits) {
    14.                     companyName = company.GetComponent(Infantry).unitName;
    15.                     new GUI.Box (Rect (0, subUnits.Length, 100, 20), "");
    16.                     new GUI.Label (Rect (0, subUnits.Length, 100, 20), companyName.ToString ());
    17.                 }
    18.              GUI.EndGroup ();
    19.         }
    20. }
    21.  
    It's creating the box fine, and at the right size, but layering the labels on top of each other. So what I need to do is find some way for the Y value of the box and label Rect to be unique for each item. Any thoughts?
     
  2. wilhelmscream

    wilhelmscream

    Joined:
    Jun 5, 2013
    Posts:
    223
    As of right now I have this about 80-90% figured out, just dealing with one last issue..... reading and displaying the string variables. It's currently able to read/display that variable from the first child in the hierarchy, but it's displaying that string for all children in the hierarchy, instead of gathering a unique string variable for each.

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var subUnitName : String;
    4. var unitList : int;
    5. var subUnits : GameObject [];
    6. var subUnit : GameObject;
    7. var unitX : float;
    8.  
    9. function Start () {
    10.     regiment = transform.parent;
    11.     unitNameStyle = GetComponentInChildren(Infantry).unitNameStyle;
    12. }
    13.  
    14. function Update () {
    15.     subUnits = GameObject.FindGameObjectsWithTag("Infantry");
    16.     unitList = subUnits.Length*20;
    17. }
    18.  
    19. function OnGUI () {
    20.     for (var subUnit : GameObject in subUnits) {
    21.         subUnitName = subUnit.GetComponent(Infantry).unitName;
    22.     }
    23.     GUI.BeginGroup (Rect (75, 563 - unitList, 100, unitList));
    24.         GUI.Box (Rect (0, 0, 100, unitList), "");
    25.         unitX = 0;
    26.         for (var i = 0; i < subUnits.Length; i++) {
    27.             new GUI.Box (Rect (0, unitX, 100, 20), "");
    28.             new GUI.Label (Rect (0, unitX, 100, 20), subUnitName.ToString ());
    29.             unitX += 20;
    30.         }
    31.      GUI.EndGroup ();
    32. }