logo SERENITY
Game Systems Foundation
Search Results for

    Show / Hide Table of Contents

    Class UnityTriggerVolumeComponent

    The base of every no-code scene component that reacts to an object walking into a trigger volume: it turns the 3D and 2D trigger messages into one enter and one exit call per visitor, and lets through only the visitors the author's layer and tag filter admit. A subclass decides what entering and leaving the volume does.

    Inheritance
    object
    UnityTriggerVolumeComponent
    Namespace: Serenity.Global.Infrastructure.Components
    Assembly: Serenity.UnityGlobal.Infrastructure.dll
    Syntax
    public abstract class UnityTriggerVolumeComponent : MonoBehaviour
    Remarks

    The volume is the trigger collider on the same GameObject (a UnityEngine.Collider or a UnityEngine.Collider2D with Is Trigger on). Unity only sends trigger messages when one of the two objects has a Rigidbody (or a CharacterController), which a moving player or enemy normally already has.

    The visitor is the object that walked in, not the collider that touched the volume: the GameObject carrying the collider's Rigidbody (or Rigidbody2D); without one, the GameObject of the CharacterController the collider belongs to (itself or a parent); without either, the collider's own GameObject. A rig with its tag and layer on the root and hitbox colliders on its children is therefore filtered on its root, and it is one visitor: it enters when its first collider enters and leaves when its last collider leaves. Hitboxes with no body of their own only reach a volume that has a Rigidbody, as a moving volume does.

    A visitor destroyed or switched off inside the volume — an enemy killed, an object returned to a pool, a collider disabled — leaves it too. In 3D Unity sends no trigger exit for any of those, so while a visitor is inside the volume checks on every fixed update whether its colliders still exist, are active and are enabled, and a visitor whose last collider is gone is reported as having left. In 2D Unity sends the exit itself, at once. With nobody inside the check returns straight away, so an empty volume does no work. A collider switched back on while inside the volume counts as inside again because Unity reports a new enter for it.

    Disabling or destroying the volume forgets every visitor inside it and then calls OnVolumeDeactivated(). Unity sends no new enter message when the volume is enabled again, so it starts empty until the next object walks in. Unity delivers trigger messages to a disabled component too; a disabled volume ignores objects that enter it, and never reports a leave for a visitor it has forgotten.

    Constructors

    UnityTriggerVolumeComponent()

    Declaration
    protected UnityTriggerVolumeComponent()

    Properties

    RequiredTag

    The tag an object needs to set this volume off; empty accepts any tag.

    Declaration
    public string RequiredTag { get; set; }
    Property Value
    Type Description
    string

    TriggeringLayers

    The layers whose objects set this volume off.

    Declaration
    public LayerMask TriggeringLayers { get; set; }
    Property Value
    Type Description
    LayerMask

    VisitorCount

    How many admitted visitors are inside the volume right now.

    Declaration
    public int VisitorCount { get; }
    Property Value
    Type Description
    int

    Methods

    Admits(GameObject)

    True when candidate passes the layer and tag filter.

    Declaration
    public bool Admits(GameObject candidate)
    Parameters
    Type Name Description
    GameObject candidate

    The visitor that touched the volume.

    Returns
    Type Description
    bool

    Whether the visitor sets this volume off.

    OnAdmittedObjectEntered(GameObject)

    Called once when an admitted visitor's first collider enters the volume.

    Declaration
    protected abstract void OnAdmittedObjectEntered(GameObject enteringVisitor)
    Parameters
    Type Name Description
    GameObject enteringVisitor

    The visitor that entered.

    OnAdmittedObjectExited(GameObject)

    Called once when an admitted visitor's last collider leaves the volume; VisitorCount no longer counts it. A visitor that left by being destroyed arrives already destroyed. Does nothing unless overridden.

    Declaration
    protected virtual void OnAdmittedObjectExited(GameObject exitingVisitor)
    Parameters
    Type Name Description
    GameObject exitingVisitor

    The visitor that left.

    OnVolumeDeactivated()

    Called when the volume is disabled or destroyed, after it has forgotten every visitor inside it. Does nothing unless overridden.

    Declaration
    protected virtual void OnVolumeDeactivated()
    In this article
    © 2026 Serenity. All Rights Reserved