Search Unity

Question Get PhysicsWorldSingleton from MonoBehaviour

Discussion in 'Physics for ECS' started by monsterrungame, Feb 19, 2023.

  1. monsterrungame

    monsterrungame

    Joined:
    Sep 27, 2019
    Posts:
    15
    Guys how to get PhysicsWorldSingleton from MonoBehaviour? I'm trying to get it by SystemAPI:
    Code (CSharp):
    1. PhysicsWorldSingleton physicsWorldSingleton = SystemAPI.GetSingleton<PhysicsWorldSingleton>();
    but got error:
    InvalidOperationException: No suitable code replacement generated, this is either due to generators failing, or lack of support in your current context
     
  2. eggsamurai

    eggsamurai

    Joined:
    Oct 10, 2015
    Posts:
    115
  3. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    625
    SystemAPI is only intended to be used inside system callback methods (OnUpdate etc), hence the names.
    Use alternative API for singletons.
     
  4. eggsamurai

    eggsamurai

    Joined:
    Oct 10, 2015
    Posts:
    115
    any alternative API for singletons exists?
     
  5. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    625
    ... Read the second page I linked.
    In any event, you should try and look for something with a search engine before asking something so simple.
     
    eggsamurai likes this.
  6. yifanchu183

    yifanchu183

    Joined:
    Jul 4, 2019
    Posts:
    41
    use
    entityManager.CreateEntityQuery(typeof(PhysicsWorldSingleton))
    with EntityQuery.GetSingleton API
     
  7. slushieboy99

    slushieboy99

    Joined:
    Aug 29, 2014
    Posts:
    88
    Does this work with the EntityManager from the DefaultGameObjectInjectionWorld?
     
  8. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    278
    It should, yes.