Create array/list of many objects(initially unknown amount) by tag

1.8k views Asked by At

I'm currently working on a radar system for my space game, and I am trying to work out how to add gameobjects, by tag, to either a list or array that can then be used in other methods. I can't do this manually because I will be procedurally generating each level with varying amounts of these objects: planets, enemies, asteroids etc. I have looked up at least 5 or so ways of doing this however I have noticed that some methods seem to be deprecated for Unity 5 and no longer work (they were given the thumbs up 3 or 4 years ago), or they just don't work the way I need them to i.e. being local to a method instead of the class. Any help will be greatly appreciated.

1

There are 1 answers

3
Aizen On BEST ANSWER
 public List<GameObject>  myListofGameObject = new List<GameObject>();



 Start(){
    myListofGameObject.AddRange(GameObject.FindGameObjectsWithTag("TagName"));
    myListofGameObject.AddRange(GameObject.FindGameObjectsWithTag("TagName2"));
    myListofGameObject.AddRange(GameObject.FindGameObjectsWithTag("TagName3"));


    foreach(GameObject gc in myListofGameObject){

           Debug.Log(gc.name);
    }
 }

Works Perfectly fine for me, Make sure to add the System class for linq generics.