Search Unity

What are possible cases where World.DefaultGameObjectInjectionWorld is null?

Discussion in 'Entity Component System' started by davenirline, Aug 31, 2020.

  1. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    982
    We're getting reports through Unity's Crashes and Exceptions page where World.DefaultGameObjectInjectionWorld is null. It happens on PCs with good specs (3Ghz CPU (Intel), 16GB Ram, Windows 10). It doesn't happen all the time as the game works for others. I'm wondering what could have caused this. Is there a PC setting that might have been turned on/off which produced errors when preparing the default world?
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    Assume you are using DefaultWorldInitialization, the only place it is set in the entire entities package is during Initialize and World.Dispose where it's set null.

    Code (CSharp):
    1.  
    2. var world = new World(defaultWorldName, editorWorld ? WorldFlags.Editor : WorldFlags.Game);
    3. World.DefaultGameObjectInjectionWorld = world;
    4.  
    5. var systemList = GetAllSystems(WorldSystemFilterFlags.Default, editorWorld);
    6. AddSystemToRootLevelSystemGroupsInternal(world, systemList, systemList.Count);
    7.  
    The only place DefaultGameObjectInjectionWorld is set in the entities package time that is going to be null is if

    1. you set it null yourself or dispose the world
    2. you call it before Initialize is called

    If you're using the automatic bootstrap, it's called in [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
    So if you had other runtime initialization that happened before this you could be calling it too early, but more likely you'd have something else in BeforeSceneLoad that would be causing it.

    I think this is unlikely though as I suspect most people manually call the world initialization, so make sure you do that before you use it?
     
    davenirline likes this.
  3. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    982
    I'm just using defaults. I don't do my own World initialization or anything like that. I'm not an advanced user. It's frustrating that it's not null for most machines but it is null for a few. Do you think that it's safer to instantiate the default world myself.

    Edit:
    I'm mostly storing the World on Awake() of my MonoBehaviours that use it.
     
    Last edited: Aug 31, 2020
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    That's likely your issue. It's been a while since I've used a World in a monobehaviour, but from memory I'm not sure World is guaranteed to be initialized before Awake triggers on all devices.
     
    davenirline and MadeFromPolygons like this.
  5. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    982
    What is the safest way then? I'm thinking of using my own static world resolver which uses World.DefaultGameObjectInjectionWorld if it exists. But if not, I'll prepare one on my own and use that instance instead.
     
  6. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    just don't call it in awake?!
    Start() was designed to access data outside of itself
    (there's also no real need to cache it except to shorten your lines)
     
    Last edited: Aug 31, 2020
  7. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    982
    I saw some calls that uses World.DefaultGameObjectInjectionWorld directly that's not on Awake().

    Upon further inspection of the submitted log, I see this somewhere at the beginning:

    UnloadTime: 0.526200 ms
    DefaultWorldInitialization failed loading assembly: C:\Program Files (x86)\Steam\steamapps\common\AcademiaSchoolSimulator\Academia64_Data\Managed\Assembly-CSharp.dll
    0x00007FFB2D993C4C (UnityPlayer)
    0x00007FFB2D996F73 (UnityPlayer)
    0x00007FFB2D98565D (UnityPlayer)
    0x00007FFB2E23126E (UnityPlayer) UnityMain
    0x00007FFB2DDA17A1 (UnityPlayer) UnityMain
    0x000001F2EE9EECAE (Mono JIT Code) (wrapper managed-to-native) UnityEngine.DebugLogHandler:Internal_Log (UnityEngine.LogType,UnityEngine.LogOption,string,UnityEngine.Object)
    0x000001F2EE9EE46B (Mono JIT Code) UnityEngine.DebugLogHandler:LogFormat (UnityEngine.LogType,UnityEngine.Object,string,object[])
    0x000001F2EE9EE1B0 (Mono JIT Code) UnityEngine.Logger:Log (UnityEngine.LogType,object)
    0x000001F2EE9EDEDA (Mono JIT Code) UnityEngine.Debug:LogWarning (object)
    0x000001F2EE9EDE5B (Mono JIT Code) Unity.Debug:LogWarning (string)
    0x000001F2EE9E79EB (Mono JIT Code) Unity.Entities.DefaultWorldInitialization:GetTypesDerivedFrom (System.Type)
    0x000001F2EE9E736B (Mono JIT Code) Unity.Entities.DefaultWorldInitialization:CreateBootStrap ()
    0x000001F2EE9E610B (Mono JIT Code) Unity.Entities.DefaultWorldInitialization:Initialize (string,bool)
    0x000001F2EE9E6043 (Mono JIT Code) Unity.Entities.AutomaticWorldBootstrap:Initialize ()
    0x000001F17F3BEC7D (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void (object,intptr,intptr,intptr)
    0x00007FFB2CE3D660 (mono-2.0-bdwgc) mono_get_runtime_build_info
    0x00007FFB2CDC28E2 (mono-2.0-bdwgc) mono_perfcounters_init
    0x00007FFB2CDCB93F (mono-2.0-bdwgc) mono_runtime_invoke
    0x00007FFB2DD3DABD (UnityPlayer) UnityMain
    0x00007FFB2DD3AEA3 (UnityPlayer) UnityMain
    0x00007FFB2DBFCAB2 (UnityPlayer) UnityMain
    0x00007FFB2DBFE07B (UnityPlayer) UnityMain
    0x00007FFB2DC16592 (UnityPlayer) UnityMain
    0x00007FFB2DC159F1 (UnityPlayer) UnityMain
    0x00007FFB2DC174CB (UnityPlayer) UnityMain
    0x00007FFB2DC16073 (UnityPlayer) UnityMain
    0x00007FFB2DC18479 (UnityPlayer) UnityMain
    0x00007FFB2DC186EE (UnityPlayer) UnityMain
    0x00007FFB2DC1D123 (UnityPlayer) UnityMain
    0x00007FFB2DC0EB1E (UnityPlayer) UnityMain
    0x00007FFB2D9BDAD3 (UnityPlayer)
    0x00007FFB2D9C195B (UnityPlayer) UnityMain
    0x00007FF796C111F2 (Academia64)
    0x00007FFB9B347BD4 (KERNEL32) BaseThreadInitThunk
    0x00007FFB9D2ECE51 (ntdll) RtlUserThreadStart

    It failed to load Assembly-CSharp.dll so CreateBootstrap() failed. How do I prevent this from happening?