Hi
I have a crash with ECS when i try to instantiate a prefab.
NullReferenceException: Object reference not set to an instance of an object.
at Unity.Entities.AttachToEntityClonerInjection.InstantiateCompanionComponentDelegate (System.Int32* srcArray, System.Int32 componentCount, Unity.Entities.Entity* dstEntities, System.Int32* dstCompanionLinkIndices, System.Int32* dstComponentLinkIds, System.Int32* dstArray, System.Int32 instanceCount, Unity.Entities.ManagedComponentStore managedComponentStore) [0x00000] in <00000000000000000000000000000000>:0
at Unity.Entities.ManagedComponentStore.Playback (Unity.Entities.ManagedDeferredCommands& managedDeferredCommands) [0x00000] in <00000000000000000000000000000000>:0
at Unity.Entities.EntityDataAccess.PlaybackManagedChanges () [0x00000] in <00000000000000000000000000000000>:0
at Unity.Entities.EntityDataAccess.EndStructuralChanges (Unity.Entities.EntityComponentStore+ArchetypeChanges& changes) [0x00000] in <00000000000000000000000000000000>:0
at Unity.Entities.EntityManager.Instantiate (Unity.Entities.Entity srcEntity) [0x00000] in <00000000000000000000000000000000>:0
at LevelData.SpawnBlock (AssetId id, UnityEngine.Vector3 position, UnityEngine.Quaternion angles) [0x00000] in <00000000000000000000000000000000>:0
at LevelData+d__21.MoveNext () [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) [0x00000] in <00000000000000000000000000000000>:0
at ECSGameSession+d__22.MoveNext () [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) [0x00000] in <00000000000000000000000000000000>:0
d__22:MoveNext()
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
I suspect this is caused from a particle system thats inside the prefab, sometimes it works, sometimes it doesn’t.
It only happens on a BUILD, in the editor its fine
Right now I’m on Unity 6.0.31 but this same issue happens with 2022.3.x
As for the Entities version, currently I’m on 1.3.8 but again, this still happens with 1.2.x
And yes, I remember in previous versions of entities (1.1.x I believe), by removing all prefabs with particles I “fixed” a crash in a build. But I never understood the actual issue, and I couldn’t even make a decent repro project. Maybe this is a related issue, just with different symptoms
I ran into this issue as well, but found a workaround.
I have code like the following (paraphrased from my project):
class EnemyAuthoring : MonoBehaviour {
// assigned to a separate prefab with a VisualEffect component
public GameObject deathVfxPrefab;
class Baker : Baker<EnemyAuthoring> {
public override void Bake(EnemyAuthoring authoring) {
AddComponent(GetEntity(authoring), new Enemy { deathVfx = GetEntity(authoring.deathVfx) });
}
}
}
struct Enemy : IComponentData {
public Entity deathVfx;
}
class SpawnerAuthoring : MonoBehaviour {
public List<EnemyAuthoring> enemies;
class Baker : Baker<SpawnerAuthoring> {
public override void Bake(SpawnerAuthoring authoring) {
var buffer = AddBuffer<SpawnerEntry>(GetEntity(authoring));
foreach(var enemy in enemies)
buffer.Add(new SpawnerEntry { spawnable = GetEntity(enemy) });
}
}
}
struct SpawnerEntry : IBufferElementData {
public Entity spawnable;
}
I have a SpawnerAuthoring component in a subscene, and spawn entities through references in the SpawnerEntry buffer.
Instantiating the deathVfx entity works great in the editor but crashes on Android device builds with the same stack trace as the OP (in AttachToEntityClonerInjection.InstantiateCompanionComponentDelegate).
By chance I was testing streaming enemy data and found that turning public Entity spawnable into public EntityPrefabReference spawnable and doing runtime loading for it bypasses this error.
struct SpawnerEntry : IBufferElementData {
public EntityPrefabReference spawnable;
}