Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Null Reference Exception Re-loading level

Discussion in 'FPS.Sample Game' started by lclandrews, Sep 3, 2020.

  1. lclandrews

    lclandrews

    Joined:
    Jun 4, 2020
    Posts:
    1
    Hi All,

    So I am currently running into some issues with the FPS sample. When running the client, I can connect to a server for the first time completely fine. However if I exit back to the main menu and attempt to join the server again I am greeted with a null reference exception and the game stops execution. And at this point I am given the following call stack:

    MissingReferenceException: The object of type 'Camera' has been destroyed but you are still trying to access it.
    Your script should either check if it is null or you should not destroy the object.
    UpdatePlayerCameras.OnUpdate () (at Assets/Scripts/Game/Systems/PlayerCamera/PlayerCameraSystem.cs:46)
    Unity.Entities.ComponentSystem.InternalUpdate () (at Library/PackageCache/com.unity.entities@0.0.12-preview.24/Unity.Entities/ComponentSystem.cs:475)
    Unity.Entities.ScriptBehaviourManager.Update () (at Library/PackageCache/com.unity.entities@0.0.12-preview.24/Unity.Entities/ScriptBehaviourManager.cs:83)
    PlayerModuleClient.CameraUpdate () (at Assets/Scripts/Game/Modules/Player/PlayerModuleClient.cs:231)
    ClientGameWorld.LateUpdate (ChatSystemClient chatSystem, System.Single frameDuration) (at Assets/Scripts/Game/Main/ClientGameLoop.cs:259)
    ClientGameLoop.LateUpdate () (at Assets/Scripts/Game/Main/ClientGameLoop.cs:849)
    Game.LateUpdate () (at Assets/Scripts/Game/Main/Game.cs:666)

    I attempted to put in a band aid fix and simple check whether or not the Camera and PlayerCamera components at line 42 in PlayerCameraSystem are null before accessing their properties:

    Code (CSharp):
    1.  
    2. var cameraArray = Group.GetComponentArray<Camera>();
    3.         var playerCameraArray = Group.GetComponentArray<PlayerCamera>();
    4.         for (var i = 0; i < cameraArray.Length; i++)
    5.         {
    6.             var camera = cameraArray[i];
    7.             var playerCamera = playerCameraArray[i];
    8.             if (camera != null && playerCamera != null)
    9.             {
    10.                 var settings = playerCamera.cameraSettings;
    11.                 var enabled = settings.isEnabled;
    12.                 var isActive = camera.gameObject.activeSelf;
    13.                 if (!enabled)
    14.                 {
    15.                     if (isActive)
    16.                     {
    17.                         Game.game.PopCamera(camera);
    18.                         camera.gameObject.SetActive(false);
    19.                     }
    20.                     continue;
    21.                 }
    22.  
    While this does prevent the above null reference exception I am now presented with the following issue elsewhere:

    ArgumentException: The entity does not exist
    Unity.Entities.EntityDataManager.AssertEntityHasComponent (Unity.Entities.Entity entity, Unity.Entities.ComponentType componentType) (at Library/PackageCache/com.unity.entities@0.0.12-preview.24/Unity.Entities/EntityDataManager.cs:369)
    Unity.Entities.EntityDataManager.AssertEntityHasComponent (Unity.Entities.Entity entity, System.Int32 componentType) (at Library/PackageCache/com.unity.entities@0.0.12-preview.24/Unity.Entities/EntityDataManager.cs:377)
    Unity.Entities.EntityManager.GetComponentData[T] (Unity.Entities.Entity entity) (at Library/PackageCache/com.unity.entities@0.0.12-preview.24/Unity.Entities/EntityManager.cs:635)
    Dead_RequestActive.Update (Unity.Entities.Entity entity, CharBehaviour charAbility, AbilityControl abilityCtrl, CharBehavior_Dead+InternalState internalState) (at Assets/Scripts/Game/Modules/Character/Behaviours/CharBehavior_Dead.cs:43)
    BaseComponentDataSystem`3[T1,T2,T3].OnUpdate () (at Assets/Scripts/ECS/BaseComponentDataSystem.cs:263)
    Unity.Entities.ComponentSystem.InternalUpdate () (at Library/PackageCache/com.unity.entities@0.0.12-preview.24/Unity.Entities/ComponentSystem.cs:475)
    Unity.Entities.ScriptBehaviourManager.Update () (at Library/PackageCache/com.unity.entities@0.0.12-preview.24/Unity.Entities/ScriptBehaviourManager.cs:83)
    SystemCollection.Update () (at Assets/Scripts/Game/Modules/Character/CharacterModuleShared.cs:18)
    CharacterModuleShared.AbilityRequestUpdate () (at Assets/Scripts/Game/Modules/Character/CharacterModuleShared.cs:78)
    ClientGameWorld.PredictionUpdate () (at Assets/Scripts/Game/Main/ClientGameLoop.cs:350)
    ClientGameWorld.Update (System.Single frameDuration) (at Assets/Scripts/Game/Main/ClientGameLoop.cs:168)
    ClientGameLoop.UpdatePlayingState () (at Assets/Scripts/Game/Main/ClientGameLoop.cs:836)
    StateMachine`1[T].Update () (at Assets/Scripts/Utils/StateMachine.cs:26)
    ClientGameLoop.Update () (at Assets/Scripts/Game/Main/ClientGameLoop.cs:623)
    Game.Update () (at Assets/Scripts/Game/Main/Game.cs:619)

    Not being overly familiar with ECS I am struggling a little to understand the exact root cause of the issue. I have seen a few other posts on this forum where other people have experienced this also but as of yet I haven't seen a solution.

    If anyone is able to help it would be greatly appreciated

    Luke.