Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Obtaining PhysicsWorldSingleton in OnCreate InvalidOperationException: GetSingleton<Unity.Physics.Ph

Discussion in 'Physics for ECS' started by koirat, Jan 20, 2024.

  1. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,098
    So basically when I try to obtain PhysicsWorldSingleton in one of my systems OnCreate like this:
    Code (csharp):
    1.  
    2. public void OnCreate(ref SystemState state)
    3. {
    4.     PhysicsWorldSingleton pws = SystemAPI.GetSingleton<PhysicsWorldSingleton>();
    5. }
    6.  
    I get:
    InvalidOperationException: GetSingleton<Unity.Physics.PhysicsWorldSingleton>() requires that exactly one entity exists that match this query, but there are none. Are you missing a call to RequireForUpdate<T>()? You could also use TryGetSingleton<T>()

    It does not throw exceptions when I obtain it in OnUpdate.
    Are there some rules regarding usage ?
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,788
    The singleton is created in another systems oncreate

    If your system is created before this system then it won't exist yet

    You can use [CreateAfter(T)] to get around this however I'm not sure why you need the singleton in oncreate considering the physics world will be empty until an onupdate at least.
     
    daniel-holz and koirat like this.
  3. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,098
    I have got navigation grid and I wanted to populate it using static collider from the physics world.
    I wanted to do it quite early so units can be placed in proper cells.
     
  4. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,098
    Do you know how to "catch this moment" when all physics is ready and all things on my scene are loaded ?
    So I can run my system right after it ?
     
  5. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    326
    You can add your system to the AfterPhysicsSystemGroup.
     
    koirat likes this.
  6. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,098
    I have tried this one and some other groups also.

    Inside OnCreate(){...}
    This:
    PhysicsWorldSingleton pws = SystemAPI.GetSingleton<PhysicsWorldSingleton>();
    Is properly returned.
    Unfrtunately
    pws.CollisionWorld.Bodies
    Is empty.
    It is populated when I check it during OnUpdate.

    Is it that physics world is populated with bodies during it's OnUpdate ?
     
  7. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    326
    I suggest you open the jobs section in the profiler. There you will see the jobs launched by the different systems and then processed with specific dependencies between them.
    For example, there you will see the
    BuildPhysicsWorld
    system which launches among others the two
    CreateRigidBodies
    jobs for creation of the dynamic and static bodies. These two jobs fill up the CollisionWorld.Bodies array.
    In the profiler you can see them being run.
    While these run you can't yet access the collision world.

    If you want to access it, from your system you need to create a job that references the CollisionWorld and schedule the job using the system's
    state.Dependeny
    handle in
    OnUpdate
    .

    For plenty of examples of how this is done I suggest you have a look through the demos in the PhysicsSamples project.
     
    koirat likes this.