Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Code runs fine in Editor, but fails in standalone

Discussion in 'Entity Component System' started by Cell-i-Zenit, Aug 23, 2021.

  1. Cell-i-Zenit

    Cell-i-Zenit

    Joined:
    Mar 11, 2016
    Posts:
    288
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2.   at Modules.Menues.MainMenuSceneSystem.Start () [0x00016] in <4adf5aef9b374086852bfda08310cdd4>:0
    3.   at Modules.Core.Commons.SceneManagementSystem`1[TSceneType].OnCreate () [0x00006] in <4adf5aef9b374086852bfda08310cdd4>:0
    4.   at Unity.Entities.ComponentSystemBase.CreateInstance (Unity.Entities.World world, Unity.Entities.SystemState* statePtr) [0x00029] in <02a8e9b73758432796e7d0f46dcda14c>:0
    5.   at Unity.Entities.World.AddSystem_OnCreate_Internal (Unity.Entities.ComponentSystemBase system) [0x00020] in <02a8e9b73758432796e7d0f46dcda14c>:0
    6.   at Unity.Entities.World.GetOrCreateSystemsAndLogException (System.Collections.Generic.IEnumerable`1[T] types, System.Int32 typesCount) [0x0006a] in <02a8e9b73758432796e7d0f46dcda14c>:0
    7. UnityEngine.DebugLogHandler:Internal_LogException(Exception, Object)
    8. UnityEngine.DebugLogHandler:LogException(Exception, Object)
    9. UnityEngine.Logger:LogException(Exception, Object)
    10. UnityEngine.Debug:LogException(Exception)
    11. Unity.Debug:LogException(Exception)
    12. Unity.Entities.World:GetOrCreateSystemsAndLogException(IEnumerable`1, Int32)
    13. Unity.Entities.DefaultWorldInitialization:AddSystemToRootLevelSystemGroupsInternal(World, IEnumerable`1, Int32)
    14. Unity.Entities.DefaultWorldInitialization:Initialize(String, Boolean)
    15. Unity.Entities.AutomaticWorldBootstrap:Initialize()

    if you are interested this is what is happening in MainMenuSceneSystem.Start()

    Code (CSharp):
    1.     protected override void Start()
    2.     {
    3.       EntityManager.CreateEntityWithComponent(GameSettings.Default());
    4.  
    5.       _mainCamera = Camera.main.transform;
    6.     }
    but i have some inheritance stuff going on, which means Start is getting called like this from the parent class:

    Code (CSharp):
    1. protected virtual void Start() { }
    2.  
    3.     protected sealed override void OnCreate()
    4.     {
    5.       RequireSingletonForUpdate<TSceneType>();
    6.       Start();
    7.     }
     
  2. apkdev

    apkdev

    Joined:
    Dec 12, 2015
    Posts:
    263
    AutomaticWorldBootstrap uses
    [RuntimeInitializeOnLoadMethod(BeforeSceneLoad)]
    .

    Is it possible that the scene is not yet loaded at the time you're accessing
    Camera.main
    ?
     
    nijnstein likes this.
  3. Cell-i-Zenit

    Cell-i-Zenit

    Joined:
    Mar 11, 2016
    Posts:
    288
    That was it! Thanks. I moved everything into OnStartrunning and now it works :)
     
    nijnstein and apkdev like this.