Search Unity

InvalidOperationException: Adding/removing components or changing position/rotation/velocity...

Discussion in 'Entity Component System' started by Lekret, Apr 17, 2021.

  1. Lekret

    Lekret

    Joined:
    Sep 10, 2020
    Posts:
    358
    InvalidOperationException: Adding/removing components or changing position/rotation/velocity/collider ECS data on dynamic entities during physics step.

    I understand why this is happening in terms of code, this exception is throw when I use FixedStepSimulation update group and manipulate PhysicsVelocity or Translation, but what if I need it? What am I doing wrong?

    Here is code triggering it:
    Code (CSharp):
    1. [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    2. public class PhysicsMovementSystem : SystemBase
    3. {
    4.     protected override void OnUpdate()
    5.     {
    6.         float deltaTime = Time.DeltaTime;
    7.  
    8.         Entities.ForEach((
    9.             ref PhysicsVelocity velocity, in MoveData moveData, in GravityData gravityData) =>
    10.         {
    11.             var multiplier = moveData.Speed * deltaTime;
    12.             velocity.Linear.x = moveData.X * multiplier;
    13.             velocity.Linear.z = moveData.Y * multiplier;
    14.             velocity.Linear.y = -gravityData.Strength;
    15.         }).Schedule();
    16.     }
    17. }
     
  2. Lekret

    Lekret

    Joined:
    Sep 10, 2020
    Posts:
    358
    I feel completely stupid
    Code (CSharp):
    1. [UpdateInGroup(typeof(FixedStepSimulationSystemGroup), OrderLast = true)]