#Need help with "mysterious" System.InvalidOperationException.

1 messages · Page 1 of 1 (latest)

nimble willow
#

Have a nice day, guys.

I'm having a weird experience when trying to move an entity with the error: System.InvalidOperationException: Adding/removing components or changing position/rotation/velocity/collider ECS data on dynamic entities during physics step
I created an basic capsule with extra components added: Physics Shape, Physics Body, Trigger Event Authoring and an IComponentData component of mine.

The weird thing is, I created an Unity-tutorial-based system some time ago and change its variable to work with my new IComponentData, it's normal.
But when I created a new ISystem class, and copy all the previous system class content to the new one, it returned the above error.

I tried to debug my IComponentData, Authoring and compare the systems but haven't got a clue.
Any help is appreciated, thanks for your time!

past zephyr
# nimble willow Have a nice day, guys. I'm having a weird experience when trying to move an ent...

I thing that you need to use Entities Command Buffer in those cases since you can't make structural changes during Physics step (since it would break the simulation). If you are making any structural changes in your ISystem you should do something like this:

{
    var ecbSingleton = SystemAPI.GetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>();
    var ecb = ecbSingleton.CreateCommandBuffer(state.WorldUnmanaged);

    foreach (var (item, entity) in SystemAPI.Query<...>().WithEntityAccess())
    {
        //your code
        ecb.AddComponent<...>(entity);
    }

}``` 

I believe that grabbing the `EndSimulationEntityCommandBufferSystem.Singleton` will ensures that we avoid adding entities during the simulation step.
I am not sure if this solution is entirely correct though. Probably someone more experienced can correct me with this 😅
nimble willow
frigid tiger
#

You should be mutating LocalTransform in Before/AfterPhysicsSystemGroup or anywhere outside PhysicsSystemGroup. Arbitrarily putting a system that does this during the physics update is not valid, as the error message states. If you take a look at Window > Entities > Systems, you can look at the ordering of systems. My guess is your system is placed before ExportPhysicsWorld, meaning the physics update is not yet complete and mutation is still invalid. I wouldn’t call this particularly mysterious. The error message says not to mutate during the physics step, so I would generally move such code to after PhysicsSystemGroup, or in AfterPhysicsSystemGroup if a copy of the system is needed for every custom physics world.