docs.unity3d.com
    Show / Hide Table of Contents

    Method SetSingleton

    SetSingleton<T>(T)

    Sets the value of a singleton component.

    Declaration
    [BurstCompatible(GenericTypeArguments = new Type[]{typeof(BurstCompatibleComponentData)})]
    public void SetSingleton<T>(T value)
        where T : struct, IComponentData
    Parameters
    Type Name Description
    T value

    An instance of type T containing the values to set.

    Type Parameters
    Name Description
    T

    The component type.

    Remarks

    For a component to be a singleton, there can be only one instance of that component that satisfies this query.

    Note: singletons are otherwise normal entities. The EntityQuery and ComponentSystemBase singleton functions add checks that you have not created two instances of a type that can be accessed by this singleton query, but other APIs do not prevent such accidental creation.

    To create a singleton, create an entity with the singleton component.

    For example, if you had a component defined as:

    public struct Singlet : IComponentData
    {
       public int Value;
    }

    You could create a singleton as follows:

           Entity singletonEntity = entityManager.CreateEntity(typeof(Singlet));
           entityManager.SetComponentData(singletonEntity, new Singlet { Value = 1 });

    To update the singleton component after creation, you can use an EntityQuery object that selects the singleton entity and call this SetSingleton() function:

           queryForSingleton.SetSingleton<Singlet>(new Singlet {Value = 1});

    You can set and get the singleton value from a system: see SetSingleton<T>(T) and GetSingleton<T>().

    Exceptions
    Type Condition
    InvalidOperationException

    Thrown if more than one instance of this component type exists in the world or the component type appears in more than one archetype.

    See Also
    GetSingleton<T>()
    GetSingletonEntity()
    In This Article
    • SetSingleton<T>(T)
    Back to top
    Terms of use
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023