Skip to content

6. Unity Tags and Physics Layers

Raphaël Tétreault edited this page Oct 24, 2025 · 7 revisions

Overview

Because everyone will be making unique games with unique layers and tags, we cannot rely on the Unity tag system. Unity's solution for these are both limited in this context. Of great issue is that if two teams make new tags, when trying to merge, the tags will overlap and clobber each other. Each new merge therefore break all previous mini games.


Tag Solution

We must replace the default tagging solution with something else.

// Unity's default solution
Component component = GetComponent<ExampleScript>();
bool isPlayer = component.CompareTag("Player");
if (isPlayer)
{
    // Do something
}

As mentioned, we cannot rely on this. Instead, we can create empty scripts and check for the script on a GameObject.

// Let's create a tag script. It's just an empty class in our namespace.
namespace MiniGameCollection.Games2024.Team00
{
    public class TagPlayer { }
}

Once we have the script set up and placed on the objects we wish to tag, we can then check for them by doing this.

// Our solution
Component component = GetComponent<TagPlayer>();
bool isPlayer = component != null;
if (isPlayer)
{
    // Do something
}

For a more robust approach, we can use GetComponentInChildren<Type> to look from that component type starting from it then searching all children in the hierarchy, or GetComponentInParent<Type> to look from that component type starting from it then searching all parent in the hierarchy.


Physics Layer Solution

You are essentially locked into the pre-existing physics layers and cannot add your own. A few are provided which you can leverage. Below is a screenshot of the collision matrix. If you need layers that interact with others or just themselves, you have some options.

Clone this wiki locally