Skip to content

Singletons ​

For a component that lives on exactly one entity (game state, camera, player):

csharp
ref var state = ref world.GetSingleton<GameState>();   // throws unless exactly one holder
Entity holder = world.GetSingletonEntity<GameState>();
bool exists = world.HasSingleton<GameState>();

A singleton is an ordinary component; there is no separate registration. It exists while exactly one entity holds it:

csharp
world.CreateEntity(new GameState());          // one holder: the singleton exists

world.GetSingleton<GameState>().Score += 10;  // ref access, writes go straight to the pool
  • GetSingleton<T>() returns a ref to the component and throws unless exactly one entity has it.
  • GetSingletonEntity<T>() returns the holder's Entity, with the same check.
  • HasSingleton<T>() is true only when exactly one entity has the component — false for zero and for two or more.