Class TagFieldAttribute
Marks a string field so the Inspector renders it as a tag selection dropdown
populated from the project's defined tags instead of a raw text input.
Inherited Members
Namespace: Scylla.Core.Attributes
Assembly: ScyllaCore.dll
Syntax
[AttributeUsage(AttributeTargets.Field)]
public sealed class TagFieldAttribute : PropertyAttribute
Remarks
When this attribute is applied to a serialized string field, the custom
property drawer (TagFieldDrawer) replaces the default text field with a
popup listing every tag currently defined in the project's Tags and Layers
settings. The drawer uses Unity's built-in EditorGUI.TagField internally,
so it automatically stays in sync whenever tags are added or removed in the
Project Settings. The selected tag string is stored directly in the backing field,
making it immediately usable with GameObject.CompareTag,
GameObject.FindWithTag, and similar Unity APIs.
Using this attribute instead of a raw string field provides two concrete
benefits:
-
It prevents typos: the designer selects from a curated list of valid
tag names rather than typing free-form text. A misspelled tag would
silently fail at runtime with
CompareTag, whereas this attribute makes invalid selections impossible through the Inspector. - It enables easier discovery: all tags used across a project can be found through the Project Settings tag list, and the dropdown makes refactoring more visible when tags are renamed or removed.
Field type requirement: The decorated field must be of type string.
If the attribute is placed on any other type, the Inspector will display a red
error help box and the field will not be editable until the type is corrected.
Runtime usage note: Always prefer GameObject.CompareTag(tag) over
the equality operator (gameObject.tag == tag) at runtime. CompareTag
avoids a string allocation, making it suitable for use inside OnCollisionEnter
and other frequently called callbacks.
public class CollisionHandler : MonoBehaviour
{
/* Dropdown showing all project tags; stores the tag name as a string. */
[TagField]
[SerializeField] private string _playerTag;
[TagField]
[SerializeField] private string _enemyTag;
private void OnCollisionEnter(Collision collision)
{
/* Use CompareTag at runtime to avoid allocations. */
if (collision.gameObject.CompareTag(_playerTag))
{
HandlePlayerCollision(collision);
}
else if (collision.gameObject.CompareTag(_enemyTag))
{
HandleEnemyCollision(collision);
}
}
}