docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Method GetOrAddComponent

    GetOrAddComponent<T>(GameObject, bool)

    Get a typed component from the gameobject or add it if it doesn't already exist.

    Declaration
    public static T GetOrAddComponent<T>(GameObject go, bool insertFirst = false) where T : Component
    Parameters
    Type Name Description
    GameObject go

    The gameobject to get/add the component from/to.

    bool insertFirst

    Whether we insert the component as the first on the list for the gameobject.

    Returns
    Type Description
    T

    The component that we got or added.

    Type Parameters
    Name Description
    T

    The component type.

    In This Article
    Back to top
    Copyright © 2023 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)