• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by pastersteli · Aug 06, 2021 at 05:45 PM · dots

error DC0001: Entities.ForEach Lambda expression uses field 'commandBuffer in a reference type'. Either assign the field to a local outside of the lambda expression and use that instead, or use .WithoutBurst() and .Run()

I have a shader for dissolve effect and end of it need to destroy obj. How can ı assign variable for newly added data component.

My error:

  error Assets\Scripts\Systems\DissolveController.cs(33,9): error DC0001: Entities.ForEach Lambda expression uses field 'commandBuffer in a reference type'. Either assign the field to a local outside of the lambda expression and use that instead, or use .WithoutBurst() and .Run()

Here is my code:

 using Unity.Entities;
 using Unity.Jobs;
 using Unity.Mathematics;
 using Unity.Physics;
 using Unity.Transforms;
 using Unity.Collections;
 using UnityEngine;
 using Unity.Rendering;
 
 [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
 public class DissolveController : SystemBase
 {
     EndSimulationEntityCommandBufferSystem _commandBufferSystem;
     public EntityCommandBuffer commandBuffer;
 
     protected override void OnCreate()
     {
         _commandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
     }
 
     protected override void OnUpdate()
     {
         float deltaTime = Time.DeltaTime;
 
         commandBuffer = _commandBufferSystem.CreateCommandBuffer();
         Entities.ForEach((ref Entity entity,ref DissolveValue dissolveValue, ref DissolveData data) =>
             {
                 if (data.dissolveValue < 1)
                 {
                     data.dissolveValue += data.speed;
                     dissolveValue.Value = data.dissolveValue;
                 }
                 else
                 {
                     commandBuffer.DestroyEntity(entity);
                 }
             })
             .Run();
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by andrew-lukasik · Aug 06, 2021 at 07:03 PM

error DC0001: Entities.ForEach Lambda expression uses field 'commandBuffer in a reference type'. Either assign the field to a local outside of the lambda expression and use that instead, or use .WithoutBurst() and .Run()

Error says it all: You are referencing an class field ( DissolveController::commandBuffer) and this is not allowed. Also - a command buffer to work must be re-created every update, so 0 reasons to hoard it like that.

 using Unity.Entities;
 using Unity.Jobs;
 
 [UpdateInGroup( typeof(FixedStepSimulationSystemGroup) )]
 public class DissolveController : SystemBase
 {
     EndSimulationEntityCommandBufferSystem _commandBufferSystem;
     protected override void OnCreate()
     {
         _commandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
     }
     protected override void OnUpdate()
     {
         float deltaTime = Time.DeltaTime;
 
         var commandBuffer = _commandBufferSystem.CreateCommandBuffer().AsParallelWriter();
         Entities
             .WithName("give_me_a_name_job")// "_job" postfix is mandatory (also no spaces), name will help with debugging later on
             .ForEach( ( ref DissolveValue dissolveValue , ref DissolveData data , in int entityInQueryIndex , in Entity entity ) =>
             {
                 if( data.dissolveValue<1 )
                 {
                     data.dissolveValue += data.speed;
                     dissolveValue.Value = data.dissolveValue;
                 }
                 else
                 {
                     commandBuffer.DestroyEntity( entityInQueryIndex , entity );
                 }
             })
             .WithBurst()
             .ScheduleParallel();
         
         _commandBufferSystem.AddJobHandleForProducer( this.Dependency );
     }
 }
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pastersteli · Aug 06, 2021 at 07:52 PM 0
Share

why we are using with name ? Thank you very much, God needs to give you extra years for helping people :D

How can I add value there for dissolvedata? I can't add default value for struct.

     [ReadOnly] public ComponentDataFromEntity<CollideAndDestroy> collideAndDestroyGroup;
     [ReadOnly] public ComponentDataFromEntity<BallFollowData> ballFollowGroup;
     public EntityCommandBuffer commandBuffer;
     public void Execute(CollisionEvent ctx)
     {
         
         Entity A = ctx.EntityA, B = ctx.EntityB;
         if (collideAndDestroyGroup.HasComponent(A))
             if (ballFollowGroup.HasComponent(B))
                 commandBuffer.AddComponent<DissolveData>(B);
         
                 
     }
avatar image andrew-lukasik pastersteli · Aug 06, 2021 at 08:16 PM 1
Share

Try

 commandBuffer.AddComponent( B , new DissolveData{
     Value = someValue
 } );
avatar image pastersteli · Aug 06, 2021 at 08:21 PM 0
Share

I tried this , dont adding component

 [BurstCompile]
 struct CollideAndDestroyJob : ICollisionEventsJob
 {
     [ReadOnly] public ComponentDataFromEntity<CollideAndDestroy> collideAndDestroyGroup;
     [ReadOnly] public ComponentDataFromEntity<BallFollowData> ballFollowGroup;
     public EntityCommandBuffer commandBuffer;
     public void Execute(CollisionEvent ctx)
     {
         
         Entity A = ctx.EntityA, B = ctx.EntityB;
         if (collideAndDestroyGroup.HasComponent(A))
             if (ballFollowGroup.HasComponent(B))
                 commandBuffer.AddComponent(B,new DissolveData() { speed = 0.1f,dissolveValue=0f});
         
                 
     }
 }

avatar image andrew-lukasik pastersteli · Aug 07, 2021 at 08:31 AM 1
Share

Idk, it works on my machine. You probably forgot to commands.RemoveComponent<BallFollowData>(B); so it won't repeat ad infinitum.

 if( collideAndDestroyGroup.HasComponent(A) )
 if( ballFollowGroup.HasComponent(B) )
 {
     commandBuffer.RemoveComponent<BallFollowData>( B );
     commandBuffer.AddComponent( B , new DissolveData{ speed = 0.1f,dissolveValue=0f } );
 }
avatar image pastersteli andrew-lukasik · Aug 09, 2021 at 01:05 AM 0
Share

Problem solved and I am here with a new question. About counting entities

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

123 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[ECS] Create Entities from Job ( IJobForEachWithEntity) using an EntityCommandBuffer 1 Answer

Best way to get data from a Monobheaviour in a JobComponentSystem? 2 Answers

[ECS] DefaultWorldInitialization.Initialize take 50MB 1 Answer

Unity (ECS & DOTS) Entity Sinking 0 Answers

How to make singleplayer/multiplayer game with DOTS 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges