- Home /
How will I use the && function or something similar when using tags in code?
I dont know why I even ask questions here, but here we go.... How do I make GameObject[] go = GameObject.FindGameObjectsWithTag("Ally"); I basicly want two tags GameObject[] go = GameObject.FindGameObjectsWithTag("Ally && Enemy");
As an addition to the answers below, the &&
is a logical AND operator. It can't be used for combining whatever two things the way the word and is used in spoken language. It can only be used to produce the boolean AND result between two expressions that can be evaluated as booleans.
Where ever in code you need multiple values to be passed around to accomplish a task, the only way to do it is to use collection: an array, a List, a HashSet etc. or to have a method that takes in the needed amount of parameters. FindObjects doesn't have an overloaded version that would rake in multiple parameters nor an overload that takes in an array, so you have to do multiple calls.
Answer by Bilelmnasser · Sep 26, 2017 at 02:19 PM
//If it is a list, you can also use AddRange method.
var listB = new List<int>{3, 4, 5};
var listA = new List<int>{1, 2, 3, 4, 5};
listA.AddRange(listB); // listA now has elements of listB also.
//If you need new list (and exclude the duplicate), you can use union
var listB = new List<int>{3, 4, 5};
var listA = new List<int>{1, 2, 3, 4, 5};
var listFinal = listA.Union(listB);
//If you need new list (and include the duplicate), you can use concat
var listB = new List<int>{3, 4, 5};
var listA = new List<int>{1, 2, 3, 4, 5};
var listFinal = listA.Concat(listB);
Thank you all for the answers... Both work with some tweaking thanx...
Answer by wornTunic · Sep 26, 2017 at 02:03 PM
You can't find multiple tags, but you can merge your lists, so, do it like this:
List<GameObject> allyList = GameObject.FindGameObjectsWithTag("Ally").ToList();
List<GameObject> enemyList = GameObject.FindGameObjectsWithTag("Enemy").ToList();
List<GameObject> allyAndEnemyList = allyList.Union(enemyList);
Your answer
![](https://koobas.hobune.stream/wayback/20220612135653im_/https://answers.unity.com/themes/thub/images/avi.jpg)