Getting an error that I don't understand

I’m getting this error, and unfortunately I don’t know which change in my code triggered it (it appeared after I did a massive amount of changes while implementing NetCode-related logic to my project). According to what the console says, the error stems from a call to collisionWorld.CastCollider() that I do in one of my systems. As far as I can tell, there seems to be no [WriteOnly] anywhere in all of the code that leads up to BoundingVolumeHierarchy.cs line 26

The only possibly-valuable detail I can think of is that my call to CastCollider() in KinematicCharacterUtilities.cs line 840 is part of the Ghost Prediction Group, and I’m getting the CollisionWorld from NetCode’s PhysicsWorldHitstory system. But I’m not sure enough about the meaningfulness of this to post it in the Netcode forums

This is kind of a long shot, but any ideas would be appreciated.

I had a similar issue (but not with BoundingVolumeHierarchy.Node) when I did some big code changes recently. I would recommend opening a new scene and adding only the minimum number of entities to test various systems. Start adding the minimal set of components for each system that calls collisionWorld.CastCollider() and that way you can hopefully narrow down which system this is happening in.

1 Like

Found the solution, in case it helps anyone:
I think what was happening was that I had two jobs that were trying to use the same CollisionWorld for physics queries in parallel, without explicitly specifying that the CollisionWorld was supposed to be readonly. So it seems that the problem wasn’t really about things being [WriteOnly] as the error message suggested, but rather that there were things that COULD be written to


I managed to track down the culprit by opening the Systems window and disabling systems one by one until the error didn’t happen anymore. I zeroed in on a system that was mentioned nowhere in the error message: WeaponsSystem (one of my own systems)

WeaponsSystem and CharacterSystem (the system responsible for the original error message) were the only two systems in my project using the CollisionWorld from PhysicsHistorySystem. However, despite the fact that disabling WeaponsSystem solved the error, the true fix was in CharacterSystem

In WeaponsSystem, I was getting the CollisionWorld from PhysicsHistorySystem before scheduling the job.
In CharacterSystem, I was passing the PhysicsWorldHistory.CollisionHistory to the job, and getting the CollisionWorld from it inside the job.

The fix was simply getting the CollisionWorld from PhysicsHistorySystem before scheduling the job in CharacterSystem as well, just like I did in my WeaponsSystem. I also made sure that CollisionWorld was marked as readonly in both jobs.

4 Likes

Just started exploring Unity.Physics and had the same error message when trying to execute
CastCollider. Thanks to https://github.com/Unity-Technologies/EntityComponentSystemSamples/blob/master/UnityPhysicsSamples/Assets/Demos/3.%20Query/Scripts/QueryTester.cs and PhilSA answer I was able to grasp the concept.

  1. Get PhysicsWorld reference somewhere outside of Execute() thread (for example, in System’s OnUpdate() ).
ref PhysicsWorld world = ref World.DefaultGameObjectInjectionWorld.GetExistingSystem<BuildPhysicsWorld>().PhysicsWorld;
  1. Define world as an attribute insude your job
    public struct ApplyCharacterKinematic : IJobEntityBatch  
    {
        ...
        [ReadOnly] public PhysicsWorld World;
        ...
    }
  1. Pass world ref as a parameter inside your job structure inside OnUpdate,
    protected override void OnUpdate()
    {
        ref PhysicsWorld world = ref World.DefaultGameObjectInjectionWorld.GetExistingSystem<BuildPhysicsWorld>().PhysicsWorld;

        var applyCharacterKinematicJob = new ApplyCharacterKinematic{
            ...
            World = world
            ...
        };

        applyCharacterKinematicJob.Schedule(<YourEntityQuery>, this.Dependency).Complete();
    }