• 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
1
Question by GamerLordMat · Mar 10, 2020 at 05:41 PM · errordots

Doing a double loop in a job in ecs with Job Entites.forech

I am trying to do a double forech loop in Unity ECS within a JOB, a gravitaional like system. I have done it already succesfully with Unity 2018 with a IJOBParalleForEach, but the code didnt work with Unity 2019 so I tried it now with the new Job Entites.forech and it doesnt work either, I really have no idea why I get the error: error DC0001: Entities.ForEach Lambda expression uses field 'physicsMasses'. Either assign the field to a local outside of the lambda expression and use that instead, or use .WithoutBurst() and .Run().

 using System.Collections; using System.Collections.Generic; using UnityEngine; using Unity.Entities; using Unity.Jobs; using Unity.Transforms; using Unity.Mathematics; using Unity.Physics; using Unity.Collections; 
 public class SwarmSystem : JobComponentSystem {
         public float time;
         public float gravK = 6.674f;
         EntityManager entityManager;
         [DeallocateOnJobCompletion]  NativeArray<Translation> translations;
         [DeallocateOnJobCompletion]  NativeArray<PhysicsMass> physicsMasses;
         protected override void OnCreate()
         {
             var v = GameObject.FindObjectOfType<MyGameManager>();
             entityManager = v.entityManager;
         }
         protected override JobHandle OnUpdate(JobHandle inputDeps)
         {
 
             EntityQuery q = GetEntityQuery(ComponentType.ReadOnly<IsPartOfSwarmTag>(), ComponentType.ReadOnly<Translation>(), ComponentType.ReadOnly<PhysicsMass>());
             time = Time.DeltaTime;
             translations = q.ToComponentDataArray<Translation>(Allocator.TempJob);
             physicsMasses = q.ToComponentDataArray<PhysicsMass>(Allocator.TempJob);
     
             var job = Entities.WithAll<IsPartOfSwarmTag>().ForEach((Entity e, ref ForceData f, in PhysicsMass pm, in Translation t ) => {
     
                 for(int i = 0; i< translations.Length; i++)
                 {
                     float k = physicsMasses[i].InverseMass * pm.InverseMass * gravK;
                     float3 vec = t.Value - translations[i].Value;
                     float length = math.length(vec);
                     float potenz = math.pow(length, 3);
                     f.Value += (k / potenz) * vec;
                 }
     
             }).Schedule(inputDeps);
             return job;
         } }

ForceData is just a float3 IComponentData

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
1

Answer by andrew-lukasik · May 29, 2020 at 08:51 PM

It wants you to change it to a local variable

 var physicsMasses = q.ToComponentDataArray<PhysicsMass>(Allocator.TempJob);

or make an local alias variable and use it in your job

 var localPhysicsMasses = physicsMasses;//use localPhysicsMasses from now on



NOTE: if you make your system inherit from SystemBase you will be able to use Job semantics (imo more fitting here)

Comment
Add comment · Show 1 · 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 GamerLordMat · Aug 14, 2021 at 09:55 AM 0
Share

thanks for the answer! (old post but I totally missed to reply)

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

162 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 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

Convert to entity script causes Unity to crash? 1 Answer

How can recreate entitymanager after recompile (Unity ECS or DOTS) 0 Answers

Unity Jobs Error - "Job can only create Temp memory" 1 Answer

How to resolve "Multiple Precompiled Assemblies" error 0 Answers

ECS on Android phone issue 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