How to use 2 tags in FindGameObjectsWithTag.
I'm trying to make a restaurant game and I have 2 types of things I'd like to be able to pick up, ingredients and cooking equipment. I have 2 tags for these. How do I put them both in 1 FindGameObjectsWithTag? I think it might be something like FindGameObjectsWithTag("tag1", "tag2").
Answer by UnityCoach · May 04, 2017 at 09:38 AM
If you want to gather all objects in one list, you could simply merge the two lists.
List<GameObject> items = new List<GameObject> (FindGameObjectsWithTag ("tag1"));
items.AddRange (new List<GameObject> (FindGameObjectsWithTag ("tag2")));
If you want to give objects more than one tag, you would have to add custom properties with a script. Like
public class Categories : MonoBehaviour
{
public List<string> categories;
}
Then you could use
FindObjectsOfType< Categories >();
to find all categorised objects, and parse this with Linq to get any combination you want.
Answer by Piyush_Pandey · May 04, 2017 at 09:49 AM
Unity has no inbuilt method for that. However you can use it like this:-
GameObject[] tag_1 = GameObject.FindGameObjectsWithTag("tag1");
GameObject[] tag_2 = GameObject.FindGameObjectsWithTag("tag2");
GameObject[] final_array = tag_1.Concat(tag_2).ToArray();
This is done using Linq so plz dont forget to use the Linq namespace.
Your answer
Follow this Question
Related Questions
Turn on/off renderer for objects found with "FindGameObjectsWithTag" 1 Answer
Multiple colliders index problem,i have multi colliders, which collider is my? 2 Answers
Multiple tags for one GameObject 3 Answers
DontDestroyOnLoad() and GameObject.FindGameObjectWithTag() [C#] 1 Answer
Compare GameObject with an array 1 Answer