- Home /
Best method to manage multiple tags
I would like to know if there's an eloquent way to use multiple tags in Unity. A similar question was asked here, but that was several years ago. I feel that the accepted answer (add child GameObjects and tag them), is inelegant. There's an answer that suggests you could use compound tags (such as "enemy,champion,killable"), and then check if the term you are searching for is included in the tag, but I don't see how you can get the GameObject in the first place to check the tag. After all, FindGameObjectsWithTag() requires an exact match. I've also heard from some people that you should avoid using Find functions altogether.
So, my question to the experienced developers is this: How do you use multiple tags in Unity? Alternatively, if you prefer to avoid tags, how do you access GameObjects from other GameObjects?
It looks like you're working through this in your head as you wrote the Q. Like the last two sentences: you're worried compound tags might break Find, but then you may not use Find at all.
Seems like two Qs: one is how to get/keep lists of gameObjects. $$anonymous$$ost people just keep their own lists. The other is how to give an object multiple values. $$anonymous$$ost people use a small script on each GO.
$$anonymous$$y guess why there's no way to assign multiple tags is that, take your example: a killable enemy champion has also got hitPoints, so already has a script where you can store that stuff.
Answer by RoboBeck27 · Jul 02, 2015 at 08:44 PM
What I eventually did was create my own class called ObjectGroup that stores GameObjects, and attached instances of it to a GameObject tagged "groups". Code below:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
// A class to store objects
public class ObjectGroup : MonoBehaviour {
public string groupName;
private List<GameObject> members = new List<GameObject>();
// Let some object get the contents of a group
public static List<GameObject> FindGroup(string nameToSearch){
List<ObjectGroup> groups = new List<ObjectGroup>(GameObject.FindGameObjectWithTag ("groups").GetComponents<ObjectGroup> ());
foreach (ObjectGroup group in groups) {
if(group.groupName == nameToSearch){
return group.members;
}
}
return null;
}
}
Instances of these classes can be given a different groupName (similar to a tag), and then attached to a empty parent GameObject in the editor. Any other script can then find a group with any groupName by calling ObjectGroup.FindGroup("nameOfGroup"), and get a reference to the underlying list. The script can then modify this group in any way it needs.
The beauty of this is that a GameObject can belong to as many ObjectGroups as necessary, and it can also leave and join them at any time. The way I use this is I have scripts that enable certain behaviours add their parent GameObject to a relevant group in Start() and remove it in OnDestroy().
Answer by adriandevera · Jul 01, 2015 at 03:41 PM
Funny, I was just searching about something else and ran into a solution for you found below :P By far the best solution Ive found... http://answers.unity3d.com/questions/21664/is-it-possible-to-have-multiple-tags-or-the-like-o.html
TL;DR: Add empty gameobjects with tags as childs, then use the code to search
That's the Answer the OP already linked and described in the Q, and said they wanted to avoid.
Oh sorry Im blind, that blue "here" link was really tiny..
Your answer
Follow this Question
Related Questions
How to find one object in specific layer? 1 Answer
How does FindGameObjectsWithTag work? 3 Answers
Organize around inactive GameObjects not being findable 0 Answers
Better solution for GameObject.FindGameObjectsWithTag 2 Answers
Getcomponent error? 1 Answer