Tags and Layers

Tags and layers are names you put on nodes so that code and rendering can find or filter them. Each project defines them once, in a settings asset, and Studio offers those names wherever a node or a component asks for one.

  • A tag is a text label. A node can have any number of tags, such as Enemy or Spawn.
  • A physics layer and a render layer each give a node one of 32 numbered slots. Every slot has a name, and the two sets of slots are independent of each other.

Scenes and prefabs store tags as text and layers as slot numbers. The names live in the project's settings, so you can rename a layer without touching a single scene.

Defining them

Open Project → Project Settings → Tags and Layers. Studio shows the settings asset of the open project, which is stored as Assets/Settings/TagsAndLayersSettings.dataasset. Data assets are described in Assets.

A new project has the tag Untagged, and each of the two layer sets has 32 slots. Slot 0 is named Default, and the other slots are named Layer 1 to Layer 31 until you rename them. Edit the names to match your game, and add tags to the tag list.

Studio reports these problems as warnings in the settings and does not change the saved values:

  • A tag name must not be empty, and a repeated tag name is reported.
  • A layer name must not be empty, and a repeated name within one set is reported.
  • Each layer set must have slot 0 named Default, because the engine uses it for any node whose layer number is not named.

A node whose layer number has no name shows a warning in the Inspector. At run time the node moves to Default and the engine logs one warning for that number.

Assigning them in Studio

Select a node and use the Inspector.

  • Tags is a multi-select field with chips. Its choices are the names in the tag list of the settings.
  • Physics Layer and Render Layer are single choices, listed as index: name, such as 7: Enemies.
  • A Culling Mask on a camera or a light is a multi-select over the render layers.

Using them in code

Tags

Tags are strings in Node.Tags. Adding or removing one updates the lookup tables at once.

namespace Usercode;

public class Spawner : Component
{
    [InjectService, System.Text.Json.Serialization.JsonIgnore]
    public ISceneManager? Scenes { get; private set; }

    public override void OnStart()
    {
        Node!.Tags.Add("Spawner");

        var enemies = Node.FindAllWithTag("Enemy");
        var spawn = Scenes?.FindWithTag("Spawn");
    }
}
  • Node.FindWithTag and Node.FindAllWithTag search the scene that holds the node. They return only active nodes.
  • ISceneManager.FindWithTag and FindAllWithTag search every loaded scene and then the persistent nodes, in the order the scenes were loaded.
  • Tags match exactly, including case. FindAllWithTag returns the nodes in registration order.

Layers

A layer is set by its slot number. The number is the same in code, in scenes and in the Inspector.

node.PhysicsLayer = 7;
node.RenderLayer = 2;

Setting a number that has no name sends the node to Default, as described above. To find a slot by name in physics code, use PhysicsScene.NameToLayer, which returns -1 when no slot has that name.

Masks

A LayerMask is a set of slot numbers, one bit per slot. Build one with FromLayer and combine masks with the bitwise operators:

var targets = LayerMask.FromLayer(7) | LayerMask.FromLayer(9);
var allButThree = LayerMask.Everything & ~LayerMask.FromLayer(3);

bool isTarget = targets.Contains(node.PhysicsLayer);

LayerMask.Everything includes every slot and LayerMask.Nothing includes none. Contains ignores numbers outside 0 to 31.

Culling with cameras and lights

A camera draws only the meshes whose render layer its CullingMask contains. The renderer checks this for every mesh it gathers, so a mesh on an excluded layer is not drawn by that camera.

var camera = Node!.GetComponent<CameraComponent>();
if (camera is not null)
{
    camera.CullingMask = LayerMask.Everything & ~LayerMask.FromLayer(3);
}

A light also has a CullingMask, which the renderer passes to the lights it draws. This page does not describe the visual result of a light mask; try it in your scene before you depend on it.

Physics queries

The physics brick (org.mass4.turian.physics) filters its queries with a LayerMask over the physics layers. Get the scene from its injected service, as the physics example does:

using Turian.Engine.Physics;

namespace Usercode;

public sealed class Sensor : Component
{
    [InjectService(Optional = true), JsonIgnore]
    public PhysicsScene? Physics { get; private set; }

    public bool IsTouchingEnemy(Vector3 center)
    {
        if (Physics is null)
        {
            return false;
        }

        var mask = Physics.GetMask("Enemy", "Ground");
        return Physics.CheckSphere(center, 0.5f, mask);
    }
}

GetMask skips names that match no physics slot. The query methods take an optional LayerMask after their shape parameters, including Raycast, RaycastAll, SphereCast, BoxCast, CheckSphere, OverlapSphere and OverlapBox. Leave the mask out to query every layer.

Layers also drive the collision matrix. PhysicsScene.IgnoreLayerCollision(a, b) stops two physics layers from colliding for the current session only. The project's matrix is stored in the physics settings, and PhysicsSettings.SetLayerCollision changes both directions of a pair.

Changing Node.PhysicsLayer at run time takes effect on the next physics step, as the physics example shows.

Gotchas

  • Renaming a layer changes what Studio shows and nothing else. Nodes keep their slot numbers.
  • Each set has slots 0 to 31. Validation reports any other number.
  • Studio shows only the names in the settings, but code may add any string to Node.Tags. Keep the settings and your code in agreement, or a tag that the Inspector cannot show will be easy to miss.
  • Lookups return only active nodes. A node that is inactive, or whose ancestor is inactive, is not found.

Next steps

  • Components shows how a script reads a node's tags and layers.
  • Scenes explains the scene lookups used by ISceneManager.
  • Rendering describes the cameras and lights that use culling masks.
  • Bricks explains how to install the physics brick.

← All docs Edit this page