Search Unity

FindGameObjectsWithTag - Total Amount

Discussion in 'Scripting' started by olliecard, Aug 24, 2009.

  1. olliecard

    olliecard

    Joined:
    Nov 6, 2007
    Posts:
    39
    Hello all,

    How do I find out the total number of a particular objects (that has been tagged) in a scene? So far I am trying with FindGameObjectsWithTag.

    I want it so that...
    - If 0 objects exists, there displays no guis.
    - If 1 object exists, a GUI.Button is displayed
    - If two objects, a second gui.Button appears up alongside the first gui... and so on!

    If you click on each gui.Button, it will take you to that object wherever it is. The idea is that you are planting the objects around the map and can return to them for future reference. I can do all the position lerping, its just counting the gameobjects and checking the amount in the scene. I would imagine its a forloop based thing, but its not my strong point. Any advice would be greatly appreciated. Cheers!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Use the length of the array returned by FindGameObjectsWithTag.

    --Eric
     
  3. ROCFriesePoort

    ROCFriesePoort

    Joined:
    Mar 27, 2009
    Posts:
    107
    Like Eric said, you can do this with .Length

    Code (csharp):
    1.  
    2. function Update () {
    3. var go = GameObject.FindGameObjectsWithTag ("Player");
    4. print (go.Length);
    5. }
    6.  
     
  4. olliecard

    olliecard

    Joined:
    Nov 6, 2007
    Posts:
    39
    Thankyou, it was the length variable that I was after.

    Code (csharp):
    1.  
    2. SearchNewBuildings= GameObject.FindGameObjectsWithTag("NewBuilding");
    3. var NewBuildingCount : int = SearchNewBuildings.Length;
    4.  
    5. function Update (){
    6. if (NewBuildingCount>=1){
    7. doFunction();
    8. }
    9. //etc
    10.  
     
  5. cz2isq

    cz2isq

    Joined:
    Jul 15, 2009
    Posts:
    49
    Would you mind sharing how you scripted the behaviour of the new buttons on the GUI? I mean, say you have one button already, and a new has to come up - does it appear above/below? Is there a way to say something like "for every new button on the GUI, place it 20 pixels below the last one"?

    Cheers

    cz2isq
     
  6. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    You can effect this using the GUILayout facilities, specifically BeginVertical().
     
  7. cz2isq

    cz2isq

    Joined:
    Jul 15, 2009
    Posts:
    49
    Cool, thanks!