Adding/removing components or changing position/rotation/velocity/collide ECS data on dynamic entit

Hi,

i checked the samples and this is pretty close to what i want to do:

https://github.com/Unity-Technologies/EntityComponentSystemSamples/blob/master/UnityPhysicsSamples/Assets/Demos/5.%20Modify/Scripts/ChangeSphereColliderRadiusAuthoring.cs

i want to change the box size of an entity

here is my code:

 [UpdateBefore(typeof(BuildPhysicsWorld))]
  [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
  public class EditorCameraSystem : SystemBase
  {
    protected override void OnUpdate()
    {
        Entities.ForEach((ref PhysicsCollider physicsCollider) =>
          {
            unsafe
            {
              BoxCollider* collider = (BoxCollider*) physicsCollider.ColliderPtr;
              var geo = collider->Geometry;
              geo.Size = new float3(1, 1, 1);
              collider->Geometry = geo;
            }
          })
          .WithAll<CursorFlag>()
          .Run();
    }

but it results in the following error:

InvalidOperationException: Adding/removing components or changing position/rotation/velocity/collider ECS data on dynamic entities during physics step

i also tried to add the dependency to the buildPhysicsWorld like this, which works in some other parts of the code but not here:

Dependency = Entities.ForEach((ref PhysicsCollider physicsCollider) =>
  {
    etc...
  })
  .Schedule(Dependency);

_buildPhysicsWorld.AddInputDependency(Dependency);

Any idea what iam doing wrong here? I think i am really close to the samples, but apparently i have a mistake

You aren’t scheduling your system, just Running it. The error is coming from the physics integrity checks. Do you have other systems, running between BuildPhysicsWorld and ExportPhysicsWorld that might be changing the ECS structure of the underlying data e.g. removing or adding components or entities?

1 Like

I also tried running the system as i mentioned earlier (with the schedule(Dependency))

I get this error (and also the failing job error) when scheduling the job:

NullReferenceException: Object reference not set to an instance of an object
Unity.Physics.Collider.get_Type () (at Library/PackageCache/com.unity.physics@0.6.0-preview.3/Unity.Physics/Collision/Colliders/Physics_Collider.cs:95)

Could it be because i get a wrong reference somehow?


What does CoreTriggerSystem do?

coreTriggerSystem never runs when i activate the box size changes.

But anyway it runs at these times:

 [UpdateAfter(typeof(StepPhysicsWorld))]
  [UpdateBefore(typeof(EndFramePhysicsSystem))]
  [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]

and it is a physics collision job scheduled like this:

   Dependency = collisionEventJob.Schedule(_stepPhysics.Simulation, ref _physicsWorld.PhysicsWorld, Dependency);

      _endFramePhysicsSystem.AddInputDependency(Dependency);

and has a structure like this:

 internal struct CollisionEventJob : ITriggerEventsJob
{
    public void Execute(TriggerEvent triggerEvent)
    {
      ProcessEntity(triggerEvent.EntityA, triggerEvent.EntityB);
      ProcessEntity(triggerEvent.EntityB, triggerEvent.EntityA);
    }
}

Does ProcessEntity do something like adding/editing/removing component data?