Auto compile-time support for Unity Tags

Paul Marsh
2 min readApr 20, 2021

--

I think anyone who has used Tags or Colliders in Unity will know that feeling when you are about to write code to utilize a tag and you are not sure of the spelling or case of the tag in question. Or even you get strange run-time errors because your tag has changed but your code hasn’t, etc.

private void OnTriggerEnter2D(Collider2D collision)
{
if (CompareTag(???))
_animator.SetBool(_isDestroyedParamHash, true);
}

It started me thinking, could I get to this stage…

Intellisense support

Bit of spoiler isn’t :)

Detecting changes to the Assets

I admit I would prefer if this was a little smarter but hey I don’t want to spend too much time on this.

So now when you ever you change a tag AND PRESS SAVE, the global tag file will be updated and you will get compile time errors if you are trying to use a tag that no longer exists, or is incorrectly spelt, etc.

NB. I probably should add an AssetDatabase.Refresh() at the end but I was a little paranoid about performance so you might not see your changes immediately reflected in the Inspector.

I hope you find this useful, let me know how you get on.

--

--

Paul Marsh
Paul Marsh

Written by Paul Marsh

Unity, VR, Enterprise and .Net Developer

No responses yet