How to properly check "if entity disabled"?

I’ve setup a demo Unity project with just one entity and system. The goal of this demo is to check if the entity is disabled. Just like I’d check for any other component, I setup the system with GetComponentDataFromEntity as follows:

System.cs

using Unity.Entities;

public class System : SystemBase
{
    protected override void OnUpdate()
    {
        ComponentDataFromEntity<Disabled> disabledGroup = GetComponentDataFromEntity<Disabled>();
        Dependency = Entities.ForEach((Entity entity) =>
        {
            UnityEngine.Debug.Log(disabledGroup.Exists(entity));
        }).Schedule(Dependency);
    }
}

However, this results in a mysterious InvalidOperationException.

full traceback

InvalidOperationException: The writeable ComponentDataFromEntity<Unity.Entities.Disabled> <>c__DisplayClass_OnUpdate_LambdaJob0.JobData.disabledGroup is the same Unity.Entities.ArchetypeChunkEntityType as <>c__DisplayClass_OnUpdate_LambdaJob0.safety, two containers may not be the same (aliasing).
Unity.Entities.JobChunkExtensions.ScheduleInternal[T] (T& jobData, Unity.Entities.EntityQuery query, Unity.Jobs.JobHandle dependsOn, Unity.Jobs.LowLevel.Unsafe.ScheduleMode mode, System.Boolean isParallel) (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/IJobChunk.cs:216)
Unity.Entities.JobChunkExtensions.ScheduleSingle[T] (T jobData, Unity.Entities.EntityQuery query, Unity.Jobs.JobHandle dependsOn) (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/IJobChunk.cs:111)
System.OnUpdate () (at Assets/System.cs:38)
Unity.Entities.SystemBase.Update () (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/SystemBase.cs:414)
Unity.Entities.ComponentSystemGroup.UpdateAllSystems () (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/ComponentSystemGroup.cs:445)
UnityEngine.Debug:LogException(Exception)
Unity.Debug:LogException(Exception) (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/Stubs/Unity/Debug.cs:19)
Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/ComponentSystemGroup.cs:450)
Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/ComponentSystemGroup.cs:398)
Unity.Entities.ComponentSystem:Update() (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/ComponentSystem.cs:109)
Unity.Entities.DummyDelegateWrapper:TriggerUpdate() (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/ScriptBehaviourUpdateOrder.cs:192)

I’ve noticed this error come up before when two archetypes are indistinguishable e.g., exact same number of each field type. I suspect the issue is now because the Disabled struct is empty. I’ve searched the forums and checked all threads mentioning “disabled”, with no solution in sight. What is the proper way to check if an entity is disabled?


Some context: One solution for the above demo is to write ForEach((Entity entity, Disabled disabled) => {…}). However, this wouldn’t work for my actual need: In short, I have a parent component referencing a child entity. My goal is to check if that child entity is disabled or not. Thus, the need for GetComponentDataFromEntity.

More context: I’m trying to swap between two prefabs, depending on the state of an object. (Specifically, object is healthy or object is sick. Pick a prefab depending on healthy or sick). My naive approach is to instantiate both prefabs and disable the prefabs selectively. If more experienced folks have other suggestions, open to ideas (e.g., change mesh directly?)

6066575–657281–Assets 2.zip (8.19 KB)

Why don’t use simple: .Without or .With?

WithNone

1 Like

Entities with a Disabled component are excluded from queries. IDK if you can force them to be included but I don’t think so… also the error is because you need to capture the ComponentDataFromEntity. Add .WithReadOnly(disabledGroup) to the query or switch to .Run()

Always can.

            GetEntityQuery(new EntityQueryDesc()
            {
                All = ... ,
                Any = ... ,
                None = ... ,
                Options = EntityQueryOptions.IncludeDisabled
            });
.WithEntityQueryOptions(EntityQueryOptions.IncludeDisabled)
4 Likes

Many thanks everyone! That fixes it.