You can use empty components for that instead, way less error-prone (no chance to wrongly type the component name) and as a bonus you can easily search in the hierarchy for objects with the given component.
For layers, you can’t add a given game object to more than one layer, if you do that through code you will receive a warning about that.
Yeah, it was always weird to me that Unity limits the tags to one, because I frequently need I more than one. The DOTS has a nice system to use empty entities as a tagging system, because you can easily sort and query based on them, but if you’re not using DOTS, then you’re out of luck.
Exactly, that would be very good.
in fact a search scheme in the scene by tag, it would be very good too as we do in DOTS, to filter objects in the scene by tags.
It may be, but it would be MUCH better if it were the tags themselves, working with them is much more practical for various situations, and with the unity visual script, it would certainly be even better:)
Don’t use tags! Especially don’t use more than one!
Wanna know if it’s an Archer? Then GetComponent(). Want to not have a bunch of archer scripts, but do things in a more data-oriented way? Have a Transform[ ] archers.
If you really want to attach a bunch of arbitrary data to things, but don’t want to have a central database of the things, and don’t want that data to have types? Make a tag-component that stores all the possible tags as a bit-mask, so you’re at least not depending on spelling the tag correctly and comparing by string.
Or get one of the million free asset store things that are wrappers around things having a List tags attached to them.
It would be nice if they made tags more useful yeah, right now I completely disregard them as a feature 99% of the time.
I think I only use them to mark stuff with the EditorOnly tag rarely.
With the new changes to Camera.main performance it also made the MainCamera tag not utterly useless to me.
But if they could improve the system so that you could use multiple tags per object and have a way of checking for them by not using strings that would be pretty cool.
I’ve been using ScriptableObjects as tags for a while now, super simple to set up and super flexible.
It’s not the most performant or memory efficient option but as long as you don’t need hundreds of tags per object being checked multiple times per frame it’s perfectly fine.
Tag.cs
using System.Collections.Generic;
using UnityEngine;
namespace YourNamespaceHere
{
[CreateAssetMenu(fileName = "Tag_", menuName = "TagSystem/Tag")]
public class Tag : ScriptableObject
{
public static bool HasTag(Tag tagToCheckFor, List<Tag> tagsToCheckAgainst)
{
for (var i = 0; i < tagsToCheckAgainst.Count; i++)
{
if (tagsToCheckAgainst[i] == tagToCheckFor)
{
return true;
}
}
return false;
}
}
}
Then all you’d need is say a component with a List.
I would agree with Baste though, using tags a lot can actually bite you and I’d prefer to go with actual types.
One thing that did happen to me once was Unity just lost all references to all tags in all objects at some point, were I not using a VCS I’d have been in serious trouble. That made me pretty weary of them ever since.
Hey! We have done exactly this and it’s in 2021.1! Though I can’t recall exactly which alpha build it arrived in…
We basically generalised the same improvement we made for Camera.main so every tag has a dedicated list of associated GameObjects - no more searching for the right GameObject(s)!
EDIT: I realised I misread part of the post I replied to - we haven’t made any improvements to FindObjectOfType (that I’m aware of). Just tags