Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How do I get a collection of components instead of a collection of GameObjects?

Discussion in 'Scripting' started by Yarbius, Jul 15, 2015.

  1. Yarbius

    Yarbius

    Joined:
    Apr 29, 2014
    Posts:
    22
    I have an EnemyManager script that has a collection of Enemy objects

    Code (CSharp):
    1. internal GameObject[] enemies;
    2. enemies = GameObject.FindGameObjectsWithTag("Enemy");
    Throughout my code I am iterating the collection like this:

    Code (CSharp):
    1. Enemy enemyScript;
    2. foreach (GameObject enemy in enemies)
    3.     {
    4.     enemyScript = enemy.GetComponent<Enemy>();
    5.     enemyScript.DoSomething();
    6.     }
    How can I make enemies[] a collection of EnemyScripts?
    When I try this:

    Code (CSharp):
    1. internal Enemy[] enemies;
    2. enemies = GameObject.FindGameObjectsWithTag("Enemy").GetComponent<Enemy>();
    I get an error that System.Array does not have a definition for GetComponent

    Any help is greatly appreciated, Google has not been very helpful
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    You must first get the gameobjects
    then make another array for the components

    Alternatively, when you create your enemies (via instantiate) get the components then and add them to a list, to avoid searching for them later
     
    Yarbius and Crayz like this.
  3. Crayz

    Crayz

    Joined:
    Mar 17, 2014
    Posts:
    192
    You can cache the enemies and their Enemy component and iterate it, something like this:

    Code (csharp):
    1. private GameObject[] objects;
    2. private Enemy[] enemies;
    3.  
    4. void Start()
    5. {
    6.    objects = GameObject.FindGameObjectsWithTag("Enemy");
    7.  
    8.   enemies = new Enemy[objects.Length];
    9.  
    10.    for(int i = 0; i < objects.Length; i++)
    11.    {
    12.      enemies[i] = objects[i].GetComponent<Enemy>();
    13.    }
    14. }
    15.  
    16. void SomeFunc()
    17. {
    18.    for(int i = 0; i < enemies.Length; i++)
    19.    {
    20.      enemies[i].DoSomething();
    21.    }
    22. }
    Remember that arrays are fixed size, so if you plan on enemies dying or the amount of enemies changing anytime during gameplay you might want to use a list instead. Using a list would also clean the code up a bit.
     
    Yarbius likes this.
  4. Yarbius

    Yarbius

    Joined:
    Apr 29, 2014
    Posts:
    22
    I'll look at that. Its a turn based game so I was building a new collection each turn for just that reason. At the time I did not know I could iterate a List<> by index (Newb) and I tend to prefer arrays because they are more light weight and because I learned to program in the 80's :)