NullReferenceException when calling EntityQuery.GetSingleton<T>() in a SystemBase in 0.9.0

Since the 0.9.0 update, I’ve got a ton of exceptions like this when I call EntityQuery.GetSingleton() in a SystemBase, in a lot of different systems.

NullReferenceException: Object reference not set to an instance of an object
Unity.Entities.EntityQuery.GetFirstArchetypeIndexWithEntity (System.Int32& entityCount) (at Library/PackageCache/com.unity.entities@0.9.0-preview.6/Unity.Entities/Iterators/EntityQuery.cs:868)

It doesn’t happen 100%, but quite often anyway. Anybody else got those?

I’m also getting spammed with this after upgrading to 0.9.0.

So I fixed this by changing how I use a few of my singletons.

From:

OnCreate()
RequireForUpdate(m_LevelQuery = GetEntityQuery(ComponentType.ReadOnly<LevelData>(), ComponentType.ReadOnly<ActiveLevelTag>()));

OnUpdate()
var levelDestinationData = m_LevelQuery.GetSingleton<LevelData>();

To:

OnCreate()
RequireSingletonForUpdate<ActiveLevelTag>();

OnUpdate()
var levelEntity = GetSingletonEntity<ActiveLevelTag>();
var levelDestinationData = GetComponent<LevelData>(levelEntity);

No idea if this is more or less efficient than before, but at least it stops the exceptions being thrown.

1 Like

I do get the exceptions too, hence I downgraded back to 0.8.0 for now. I don’t know if it’s expected behaviour or if it’s a bug on their side, and documentation is non existent for 0.9.0 unfortunately.

The changelog mentions:

  • Improved performance of access to singletons through SetSingleton and GetSingleton in SystemBase (peformance is also improved through these methods on EntityQuery).

Looks like something must of broke during that optimization.

Duplicating the full changelog below since it doesn’t appear to be available here: https://docs.unity3d.com/Packages/com.unity.entities@0.9/

[0.9.0] - 2020-04-08
Added

  • public void GetCreatedAndDestroyedEntitiesAsync(NativeList state, NativeList createdEntities, NativeList destroyedEntities) detects which entities were created and destroyed since the last call to this method.
  • Added the ability to reimport a SubScene via an inspector button, which forces reconversion.
  • Added GameObjectConversionSystem.DeclareAssetDependency which expresses that the conversion result of a GameObject depends on an Asset
  • Added void EntityManager.Instantiate(NativeArray srcEntities, NativeArray dstEntities). It gives explicit control over the set of entities that are instantiated as a set. Entity references on components that are cloned to entities inside the set are remapped to the instantiated entities.
  • Added void EntityManager.CopyEntitiesFrom(EntityManager srcEntityManager, NativeArray srcEntities, NativeArray outputEntities = default). It lets you copy a specific set of entities from one World to another. Entity references on components that are cloned to entities inside the set are remapped to the instantiated entities.
  • Added assembly for Mesh Deformation data structures.

Changed

  • Systems are now constructed in two phases. First, ECS creates a new instance of all systems and invokes the constructor. Then, it invokes all OnCreatemethods. This way, you can now use World.GetExistingSystem() from inside OnCreate().
  • Systems are now destroyed in three phases. First, ECS stops all running systems (i.e. OnStopRunning() is invoked). Then it invokes all OnDestroymethods. Finally, ECS destroys all systems. This means you can perform safe and predictable cleanup of systems with cross-references to other systems.
  • EntityCommandBuffer Playback now Bursted through function pointers. When there’s a mix of unmanaged and managed commands in a single buffer, unmanaged commands will be Bursted. When there are no managed commands, each chain’s Playback is fully Bursted.
  • Entities.ForEach in a GameObjectConversionSystem no longer logs a warning if there are multiples of a queried authoring component on a matching GameObject. It now returns the first component instance of the desired type, so conversion systems can optionally call GetComponents() in order to handle multiples if desired.
  • Declaring a non-Prefab object as a referenced Prefab during conversion now emits a warning
  • Improved performance of access to singletons through SetSingleton and GetSingleton in SystemBase (peformance is also improved through these methods on EntityQuery).
  • Updated package com.unity.properties to version 1.1.0-preview.
  • Updated package com.unity.serialization to version 1.1.0-preview.
  • Updated package com.unity.platforms to version 0.2.2-preview.7.

Deprecated

  • Deprecated public T World.CreateSystem(params object[ ] constructorArguments). Please use World.AddSystem(new MySystem(myParams)); instead.
  • Deprecated LiveLinkBuildImport.GetHash/GetDependencies/GetBundlePath.

Removed

  • Removed expired API TypeManager.CreateTypeIndexForComponent()
  • Removed expired API TypeManager.CreateTypeIndexForSharedComponent()
  • Removed expired API TypeManager.CreateTypeIndexForBufferElement()
  • Removed expired API DynamicBuffer.Reserve(int)
  • Removed expired API World.Active

Fixed

  • Fix BlobAssetSafetyVerifier to generate a better error message when readonly is used with BlobAsset references.
  • Fixed incorrect comparison in EntityChunk.CompareTo().
  • SceneManager.IsSceneLoaded now works for converted entity Scenes and returns whether all sections of an entity Scene have loaded.
  • Fixed Exception in conversion code when trying to delete entities that are part of a Prefab.
  • Fixed Hybrid Component conversion failing when multiple components were added for the same GameObject.
  • Fixed use of component access methods (GetComponent/SetComponent/HasComponent) inside Entities.ForEach with nested captures.
  • Fix compilation issue when ENABLE_SIMPLE_SYSTEM_DEPENDENCIES is enabled.

Security
Known Issues

  • System groups do not currently apply to systems running as part of EntitySceneOptimizations
1 Like

My apologies. This has already been fixed in Entities but the fix did not make it into this latest release. We’ll try to publish a new version of the package with the fix ASAP.

2 Likes

Appreciated! I like being able to view the release notes before downloading & installing.

Appreciated! Cuz don’t want to update until it’s sorted. Had to go through hell to downgrade it back to 0.8.

Entities 0.9.1 is out now, release notes say that this issue fixed.

Nice one. Thanks mate!

1 Like