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 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:
    121
  3. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    837
    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:
    121
    any alternative API for singletons exists?
     
  5. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    837
    ... 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:
    89
    Does this work with the EntityManager from the DefaultGameObjectInjectionWorld?
     
  8. daniel-holz

    daniel-holz

    Unity Technologies

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