logo SERENITY
Game Systems Foundation
Search Results for

    Show / Hide Table of Contents

    Class UnityObjectUtils

    Small, generalized helpers for Unity object identity checks and component lookup, factored out of the per-service copies that used to hand-roll them (e.g. the destroyed-object-aware null check that used to live only in UnityAssetPrefetcherService).

    Inheritance
    object
    UnityObjectUtils
    Inherited Members
    object.ToString()
    object.Equals(object)
    object.Equals(object, object)
    object.ReferenceEquals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    Namespace: Serenity.Global.Infrastructure.Utils
    Assembly: Serenity.UnityGlobal.Infrastructure.dll
    Syntax
    public static class UnityObjectUtils

    Methods

    DestroyInAnyMode(Object)

    Destroys a Unity object through whichever API the current mode accepts: UnityEngine.Object.Destroy(UnityEngine.Object) while playing, UnityEngine.Object.DestroyImmediate(UnityEngine.Object) otherwise. An already-destroyed or null target is left alone.

    Declaration
    public static void DestroyInAnyMode(Object target)
    Parameters
    Type Name Description
    Object target

    The object to destroy. May be null or already destroyed.

    Remarks

    Runtime code that also runs in an editor context — a service composed by a wizard preview, a texture built by an EditMode test — cannot use either call unconditionally: Destroy is deferred and never runs outside play mode, so the object leaks, while DestroyImmediate is rejected at runtime. Prefer plain Destroy where the code genuinely only runs in a build; this exists for the paths that do both.

    GetOrAddComponent<TComponent>(Component)

    Returns the existing TComponent on sibling's GameObject, or adds one if none is present.

    Declaration
    public static TComponent GetOrAddComponent<TComponent>(this Component sibling) where TComponent : Component
    Parameters
    Type Name Description
    Component sibling
    Returns
    Type Description
    TComponent
    Type Parameters
    Name Description
    TComponent

    GetOrAddComponent<TComponent>(GameObject)

    Returns the existing TComponent on host, or adds one if none is present.

    Declaration
    public static TComponent GetOrAddComponent<TComponent>(this GameObject host) where TComponent : Component
    Parameters
    Type Name Description
    GameObject host
    Returns
    Type Description
    TComponent
    Type Parameters
    Name Description
    TComponent

    IsAlive(object)

    True when candidate is non-null — and, when it is a UnityEngine.Object, not destroyed either (Unity's "fake null" case: a managed reference to a native object that has already been destroyed is correctly treated as gone instead of passing a plain reference-null check).

    Declaration
    public static bool IsAlive(object candidate)
    Parameters
    Type Name Description
    object candidate
    Returns
    Type Description
    bool
    In this article
    © 2026 Serenity. All Rights Reserved