docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class ComponentUtils

    Utility class for working with Components.

    Inheritance
    object
    ComponentUtils
    Inherited Members
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    object.ToString()
    Namespace: Unity.XR.CoreUtils
    Assembly: Unity.XR.CoreUtils.dll
    Syntax
    public static class ComponentUtils

    Methods

    GetOrAddIf<T>(GameObject, bool)

    Gets a component from a GameObject. Optionally, adds a new component and returns it if a component of the specified type does not already exist.

    Declaration
    public static T GetOrAddIf<T>(GameObject gameObject, bool add) where T : Component
    Parameters
    Type Name Description
    GameObject gameObject

    The parent GameObject.

    bool add

    Whether to add a new component of the given type, if one does not already exist.

    Returns
    Type Description
    T

    The new or retrieved component.

    Type Parameters
    Name Description
    T

    The type of component to get or add.

    In This Article
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)