Version: Unity 7.0 Alpha (7000.0)
LanguageEnglish
  • C#

SystemAPI.GetSingletonRW

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public static RefRW<T> GetSingletonRW();

Returns

RefRW<T> The component.

Description

Gets a reference to the singleton component, for read/write access.

NOTE: this reference refers directly to the singleton's component memory. Structural changes to the chunk where the singleton resides can invalidate this reference and result in crashes or undefined behaviour if the reference is used after structural changes.

This method differs from SystemAPI.GetSingleton in the following ways:

The query that it generates requests read/write access to T, which registers the system as a writer of that component, so systems that read that component and update later wait for this system's jobs. SystemAPI.GetSingleton generates a read-only query, which registers the system as a reader instead.

It throws an exception if a job that reads or writes T is still running, whereas SystemAPI.GetSingleton throws only if a job that writes T is still running. Neither method completes the conflicting job for you, and both run this check only if safety checks are enabled. To complete the job, call EntityManager.CompleteDependencyBeforeRW first, or restructure the data dependencies.

It increments the change version of T, even if you write the same value, so queries that filter on changes to that component match the singleton entity. SystemAPI.GetSingleton leaves the change version unchanged.

Not working in Entities.ForEach, IJobEntity, and Utility methods

Additional resources: ComponentSystemBase.GetSingleton, EntityQuery.GetSingleton, EntityQuery.GetSingletonRW