(0.51) How to change the material color of an entity during instantiation in Entities.ForEach?

I am trying to do like this:

using Unity.Entities;
using Unity.Mathematics;
using Unity.Rendering;
using Unity.Transforms;
using UnityEngine;

partial class SpawnerSystem : SystemBase
{
    private BeginInitializationEntityCommandBufferSystem entityCommandBufferSystem;

    protected override void OnCreate()
    {
        entityCommandBufferSystem = World.GetOrCreateSystem<BeginInitializationEntityCommandBufferSystem>();
    }
    protected override void OnUpdate()
    {
        EntityCommandBuffer.ParallelWriter ecb = entityCommandBufferSystem.CreateCommandBuffer().AsParallelWriter();

        Entities
            .WithBurst()
            .ForEach((Entity entity, int entityInQueryIndex, in SpawnerAuthoring spawnerAuthoring) =>
            {
                for (int i = 0; i <= 10; i++)
                {
                            Entity instance = ecb.Instantiate(entityInQueryIndex, spawnerAuthoring.prefab);
                            ecb.SetComponent(entityInQueryIndex, instance, new Translation { Value = new float3(i, 0, 0) });
                            ecb.SetComponent(entityInQueryIndex, instance, new URPMaterialPropertyBaseColor { Value = new float4(255, 255, 255, 1) });
                        }
                    }
                }

                ecb.DestroyEntity(entityInQueryIndex, entity);
            }).ScheduleParallel();

        entityCommandBufferSystem.AddJobHandleForProducer(Dependency);
    }
}

But I am getting an error:

ArgumentException: System.ArgumentException: A component with type:{0} has not been added to the entity.
This Exception was thrown from a function compiled with Burst, which has limited exception support.
0x00007ff9f8ee77e3 (3f5fd1d2c9c66d89ba8dbcfacc2c11d) [EntityComponentStoreDebug.cs:296] Unity.Entities.EntityComponentStore::Unity.Entities.EntityComponentStore.AssertEntityHasComponent
0x00007ff9f8ee773a (3f5fd1d2c9c66d89ba8dbcfacc2c11d) [EntityComponentStoreDebug.cs:321] Unity.Entities.EntityComponentStore::Unity.Entities.EntityComponentStore.AssertEntityHasComponent
0x00007ff9f8ee7658 (3f5fd1d2c9c66d89ba8dbcfacc2c11d) [EntityDataAccess.cs:1110] Unity.Entities.EntityDataAccess::Unity.Entities.EntityDataAccess.SetComponentDataRaw
0x00007ff9f8ee9c30 (3f5fd1d2c9c66d89ba8dbcfacc2c11d) [EntityCommandBuffer.cs:2891] Unity.Entities.EntityCommandBuffer/PlaybackProcessor::Unity.Entities.EntityCommandBuffer.PlaybackProcessor.SetComponent
0x00007ff9f8eddaae (3f5fd1d2c9c66d89ba8dbcfacc2c11d) [EntityCommandBuffer.cs:2576] Unity.Entities.EntityCommandBuffer/EcbWalker`1<Unity.Entities.EntityCommandBuffer/PlaybackProcessor>::Unity.Entities.EntityCommandBuffer.EcbWalker`1<Unity.Entities.EntityCommandBuffer.PlaybackProcessor>.ProcessUnmanagedCommand
0x00007ff9f8edd71f (3f5fd1d2c9c66d89ba8dbcfacc2c11d) [EntityCommandBuffer.cs:2512] Unity.Entities.EntityCommandBuffer/EcbWalker`1<Unity.Entities.EntityCommandBuffer/PlaybackProcessor>::Unity.Entities.EntityCommandBuffer.EcbWalker`1<Unity.Entities.EntityCommandBuffer.PlaybackProcessor>.ProcessChain
0x00007ff9f8edd5bf (3f5fd1d2c9c66d89ba8dbcfacc2c11d) [ECBInterop.cs:34] Unity.Entities.ECBInterop._ProcessChainChunk
0x00007ff9f8edd52f (3f5fd1d2c9c66d89ba8dbcfacc2c11d) [ECBInterop.interop.gen.cs:93] Unity.Entities.ECBInterop._mono_to_burst_ProcessChainChunk$BurstManaged
0x00007ff9f8edd48f (3f5fd1d2c9c66d89ba8dbcfacc2c11d) [unknown:0] Unity.Entities.ECBInterop.Unity.Entities._mono_to_burst_ProcessChainChunk_00000416$BurstDirectCall.Invoke
0x00007ff9f8edd40f (3f5fd1d2c9c66d89ba8dbcfacc2c11d) [ECBInterop.interop.gen.cs:92] Unity.Entities.ECBInterop._mono_to_burst_ProcessChainChunk
0x00007ff9f8edd06a (3f5fd1d2c9c66d89ba8dbcfacc2c11d) 6cbe9b44f55dff161edecf05c9660a36
0x000001f17e0473c8 (Mono JIT Code) (wrapper managed-to-native) object:wrapper_native_000001EE9A2B19C0 (intptr,int,intptr,int,int)
0x000001f17e1a40da (Mono JIT Code) (wrapper delegate-invoke) <Module>:invoke_void_intptr_int_intptr_int_int (intptr,int,intptr,int,int)
0x000001f17e1a3ffd (Mono JIT Code) [ECBInterop.interop.gen.cs:98] Unity.Entities.ECBInterop:_forward_mono_ProcessChainChunk (void*,int,Unity.Entities.ECBChainPlaybackState*,int,int)
0x000001f17e1a3f33 (Mono JIT Code) [ECBInterop.interop.gen.cs:80] Unity.Entities.ECBInterop:ProcessChainChunk (void*,int,Unity.Entities.ECBChainPlaybackState*,int,int)
0x000001f17e10076b (Mono JIT Code) [EntityCommandBuffer.cs:2457] Unity.Entities.EntityCommandBuffer/EcbWalker`1<Unity.Entities.EntityCommandBuffer/PlaybackProcessor>:WalkChains (Unity.Entities.EntityCommandBuffer)
0x000001f17e0efe13 (Mono JIT Code) [EntityCommandBuffer.cs:2321] Unity.Entities.EntityCommandBuffer:PlaybackInternal (Unity.Entities.EntityDataAccess*)
0x000001f17e0ffdcb (Mono JIT Code) [EntityCommandBuffer.cs:2283] Unity.Entities.EntityCommandBuffer:Playback (Unity.Entities.EntityManager)
0x000001f17e0fefdb (Mono JIT Code) [EntityCommandBufferSystem.cs:228] Unity.Entities.EntityCommandBufferSystem:FlushPendingBuffers (bool)
0x000001f17e0feb2b (Mono JIT Code) [EntityCommandBufferSystem.cs:193] Unity.Entities.EntityCommandBufferSystem:OnUpdate ()
0x000001f17e0fd236 (Mono JIT Code) [ComponentSystem.cs:115] Unity.Entities.ComponentSystem:Update ()
0x000001f17e0fe744 (Mono JIT Code) [ComponentSystemGroup.cs:585] Unity.Entities.ComponentSystemGroup:UpdateAllSystems ()
0x000001f17e0fe343 (Mono JIT Code) [ComponentSystemGroup.cs:523] Unity.Entities.ComponentSystemGroup:OnUpdate ()
0x000001f17e0fe143 (Mono JIT Code) [DefaultWorld.cs:29] Unity.Entities.InitializationSystemGroup:OnUpdate ()
0x000001f17e0fd236 (Mono JIT Code) [ComponentSystem.cs:115] Unity.Entities.ComponentSystem:Update ()
0x000001f17e0fcffd (Mono JIT Code) [ScriptBehaviourUpdateOrder.cs:428] Unity.Entities.ScriptBehaviourUpdateOrder/DummyDelegateWrapper:TriggerUpdate ()
0x000001f18ae36c88 (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void__this__ (object,intptr,intptr,intptr)
0x00007ff9d293fe24 (mono-2.0-bdwgc) [mini-runtime.c:3445] mono_jit_runtime_invoke
0x00007ff9d287e764 (mono-2.0-bdwgc) [object.c:3066] do_runtime_invoke
0x00007ff9d287e8fc (mono-2.0-bdwgc) [object.c:3113] mono_runtime_invoke
0x00007ff6d2688164 (Unity) scripting_method_invoke
0x00007ff6d2667a04 (Unity) ScriptingInvocation::Invoke
0x00007ff6d2341f48 (Unity) ExecutePlayerLoop
0x00007ff6d2341f66 (Unity) ExecutePlayerLoop
0x00007ff6d2348009 (Unity) PlayerLoop
0x00007ff6d3293e39 (Unity) PlayerLoopController::UpdateScene
0x00007ff6d327f2db (Unity) PlayerLoopController::EnterPlayMode
0x00007ff6d328ef37 (Unity) PlayerLoopController::SetIsPlaying
0x00007ff6d32922cd (Unity) Applicatio
EntityCommandBuffer was recorded in SpawnerSystem and played back in Unity.Entities.BeginInitializationEntityCommandBufferSystem.
  at (wrapper managed-to-native) System.Object.wrapper_native_000001EE9A2B19C0(intptr,int,intptr,int,int)
  at (wrapper delegate-invoke) <Module>.invoke_void_intptr_int_intptr_int_int(intptr,int,intptr,int,int)
  at Unity.Entities.ECBInterop._forward_mono_ProcessChainChunk (System.Void* walker, System.Int32 processorType, Unity.Entities.ECBChainPlaybackState* chainStates, System.Int32 currentChain, System.Int32 nextChain) [0x00000] in D:\Software\Projects\DOTS\0.51 Spawner\Library\PackageCache\com.unity.entities@0.51.1-preview.21\Unity.Entities\ECBInterop.interop.gen.cs:98
  at Unity.Entities.ECBInterop.ProcessChainChunk (System.Void* walker, System.Int32 processorType, Unity.Entities.ECBChainPlaybackState* chainStates, System.Int32 currentChain, System.Int32 nextChain) [0x00007] in D:\Software\Projects\DOTS\0.51 Spawner\Library\PackageCache\com.unity.entities@0.51.1-preview.21\Unity.Entities\ECBInterop.interop.gen.cs:79
  at Unity.Entities.EntityCommandBuffer+EcbWalker`1[T].WalkChains (Unity.Entities.EntityCommandBuffer ecb) [0x0026e] in D:\Software\Projects\DOTS\0.51 Spawner\Library\PackageCache\com.unity.entities@0.51.1-preview.21\Unity.Entities\EntityCommandBuffer.cs:2457
  at Unity.Entities.EntityCommandBuffer.PlaybackInternal (Unity.Entities.EntityDataAccess* mgr) [0x000a7] in D:\Software\Projects\DOTS\0.51 Spawner\Library\PackageCache\com.unity.entities@0.51.1-preview.21\Unity.Entities\EntityCommandBuffer.cs:2320
  at Unity.Entities.EntityCommandBuffer.Playback (Unity.Entities.EntityManager mgr) [0x00000] in D:\Software\Projects\DOTS\0.51 Spawner\Library\PackageCache\com.unity.entities@0.51.1-preview.21\Unity.Entities\EntityCommandBuffer.cs:2282
  at Unity.Entities.EntityCommandBufferSystem.FlushPendingBuffers (System.Boolean playBack) [0x0007f] in D:\Software\Projects\DOTS\0.51 Spawner\Library\PackageCache\com.unity.entities@0.51.1-preview.21\Unity.Entities\EntityCommandBufferSystem.cs:227

Unity.Entities.EntityCommandBufferSystem.FlushPendingBuffers (System.Boolean playBack) (at Library/PackageCache/com.unity.entities@0.51.1-preview.21/Unity.Entities/EntityCommandBufferSystem.cs:294)
Unity.Entities.EntityCommandBufferSystem.OnUpdate () (at Library/PackageCache/com.unity.entities@0.51.1-preview.21/Unity.Entities/EntityCommandBufferSystem.cs:192)
Unity.Entities.ComponentSystem.Update () (at Library/PackageCache/com.unity.entities@0.51.1-preview.21/Unity.Entities/ComponentSystem.cs:114)
Unity.Entities.ComponentSystemGroup.UpdateAllSystems () (at Library/PackageCache/com.unity.entities@0.51.1-preview.21/Unity.Entities/ComponentSystemGroup.cs:583)
UnityEngine.Debug:LogException(Exception)
Unity.Debug:LogException(Exception) (at Library/PackageCache/com.unity.entities@0.51.1-preview.21/Unity.Entities/Stubs/Unity/Debug.cs:19)
Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at Library/PackageCache/com.unity.entities@0.51.1-preview.21/Unity.Entities/ComponentSystemGroup.cs:588)
Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.entities@0.51.1-preview.21/Unity.Entities/ComponentSystemGroup.cs:523)
Unity.Entities.InitializationSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.entities@0.51.1-preview.21/Unity.Entities/DefaultWorld.cs:28)
Unity.Entities.ComponentSystem:Update() (at Library/PackageCache/com.unity.entities@0.51.1-preview.21/Unity.Entities/ComponentSystem.cs:114)
Unity.Entities.DummyDelegateWrapper:TriggerUpdate() (at Library/PackageCache/com.unity.entities@0.51.1-preview.21/Unity.Entities/ScriptBehaviourUpdateOrder.cs:426)

So far I haven’t found a solution to the problem. I need to change the URP color of entity materials when instantiating, but when I try to do this, it gives an error.

When you get an error that includes {0} in it, try disabling Burst and running the code again.

I know what the issue is, but I think this will be a great exercise for you to learn to decipher Unity’s error messages. That skill is essential for DOTS.

Even without disabling Burst, I know that the error is in the absence of the URPMaterialPropertyBaseColor component. But why isn’t there? I’m using the standard prefab material URP. But in the topic where this method was described, a separate system was used to change the color, where through the EntityManager they did

EntityManager.AddComponentData(e, new URPMaterialPropertyBaseColor {Value = new float4(0, 0, 1, 1)});

Is it not possible to do this through ecb.SetComponent at the moment of instantiating the entity?

“Add” and “Set” are two different things. You want ecb.AddComponent.

Yes, but here’s where I don’t quite understand. If I instantiate an entity on which this component should already be, why can’t I do SetComponent at this moment?

The component is not there when the ECB is being played back. Why? That’s what you need to figure out. If you expect the component to be there so that you can use SetComponent, then you need to find out why it isn’t there.

By the way, AddComponent with a new value will resort to a SetComponent operation if the component is already present.

1 Like