Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Object refer. not set to an instance of an object Unity.Netcode.NetworkMetrics.GetObjectIdentifier??

Discussion in 'Netcode for GameObjects' started by yerimseni_343, Oct 21, 2023.

  1. yerimseni_343

    yerimseni_343

    Joined:
    Apr 15, 2023
    Posts:
    5
    Hello, I'm making a game in unity based on a tutorial I saw on youtube. After finishing the game, I haven't touched the project for 1 week, when I open the project and try to play it, I get this error if I press "E" or "Esc" (E- Interact; Esc- Pause) in GameScene. I couldn't find a solution on the internet, can anyone help me?

    Error:


    NullReferenceException: Object reference not set to an instance of an object
    Unity.Netcode.NetworkMetrics.GetObjectIdentifier (Unity.Netcode.NetworkObject networkObject) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/Metrics/NetworkMetrics.cs:527)
    Unity.Netcode.NetworkMetrics.TrackRpcSent (System.UInt64 receiverClientId, Unity.Netcode.NetworkObject networkObject, System.String rpcName, System.String networkBehaviourName, System.Int64 bytesCount) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/Metrics/NetworkMetrics.cs:342)
    Unity.Netcode.NetworkBehaviour.__endSendServerRpc (Unity.Netcode.FastBufferWriter& bufferWriter, System.UInt32 rpcMethodId, Unity.Netcode.ServerRpcParams serverRpcParams, Unity.Netcode.RpcDelivery rpcDelivery) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/Core/NetworkBehaviour.cs:107)
    KitchenGameManager.SetPlayerReadyServerRpc (Unity.Netcode.ServerRpcParams serverRpcParams) (at D:/DOMINO/Unity Projelerim/KitchenChaosMultiplayer/Assets/Scripts/KitchenGameManager.cs:103)
    KitchenGameManager.GameInput_OnInteractAction (System.Object sender, System.EventArgs e) (at D:/DOMINO/Unity Projelerim/KitchenChaosMultiplayer/Assets/Scripts/KitchenGameManager.cs:97)
    GameInput.Interact_performed (UnityEngine.InputSystem.InputAction+CallbackContext obj) (at D:/DOMINO/Unity Projelerim/KitchenChaosMultiplayer/Assets/Scripts/GameInput.cs:67)
    UnityEngine.InputSystem.Utilities.DelegateHelpers.InvokeCallbacksSafe[TValue] (UnityEngine.InputSystem.Utilities.CallbackArray`1[System.Action`1[TValue]]& callbacks, TValue argument, System.String callbackName, System.Object context) (at ./Library/PackageCache/com.unity.inputsystem@1.5.1/InputSystem/Utilities/DelegateHelpers.cs:46)
    UnityEngine.InputSystem.LowLevel.<>c__DisplayClass7_0:<set_onUpdate>b__0(NativeInputUpdateType, NativeInputEventBuffer*)
    UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate(NativeInputUpdateType, IntPtr)



    NullReferenceException while executing 'performed' callbacks of 'Player/Interact[/Keyboard/e]'
    UnityEngine.InputSystem.LowLevel.NativeInputRuntime/<>c__DisplayClass7_0:<set_onUpdate>b__0 (UnityEngineInternal.Input.NativeInputUpdateType,UnityEngineInternal.Input.NativeInputEventBuffer*)
    UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate (UnityEngineInternal.Input.NativeInputUpdateType,intptr)



    Codes:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5. using Unity.Netcode;
    6. using UnityEngine.SceneManagement;
    7.  
    8. public class KitchenGameManager : NetworkBehaviour
    9. {
    10.  
    11.     public static KitchenGameManager Instance { get; private set; }
    12.  
    13.     public event EventHandler OnStateChanged;
    14.     public event EventHandler OnLocalGamePaused;
    15.     public event EventHandler OnLocalGameUnPaused;
    16.     public event EventHandler OnMultiplayerGamePaused;
    17.     public event EventHandler OnMultiplayerGameUnPaused;
    18.     public event EventHandler OnLocalPlayerReadyChanged;
    19.  
    20.  
    21.     private enum State {
    22.         WaitingToStart,
    23.         CountDownToStart,
    24.         GamePlaying,
    25.         GameOver,
    26.     }
    27.  
    28.     [SerializeField] private Transform playerPrefab;
    29.  
    30.     private NetworkVariable<State> state = new NetworkVariable<State>(State.WaitingToStart);
    31.     private bool isLocalPlayerReady;
    32.     private NetworkVariable<float> countdownToStartTimer = new NetworkVariable<float>(3f);
    33.     private NetworkVariable<float> gamePlayingTimer = new NetworkVariable<float>(0f);
    34.     private float gamePlayingTimerMax = 1000f;
    35.     private bool isLocalGamePaused = false;
    36.     private NetworkVariable<bool> isGamePaused = new NetworkVariable<bool>(false);
    37.     private Dictionary<ulong, bool> playerReadyDictionary;
    38.     private Dictionary<ulong, bool> playerPausedDictionary;
    39.     private bool autoTestGamePausedState;
    40.  
    41.     public static bool interactionsEnabled = false;
    42.  
    43.     private void Awake() {
    44.         Instance = this;  
    45.  
    46.         playerReadyDictionary = new Dictionary<ulong, bool>();
    47.         playerPausedDictionary = new Dictionary<ulong, bool>();
    48.     }
    49.  
    50.     private void Start() {
    51.         GameInput.Instance.OnPauseAction += GameInput_OnPauseAction;
    52.         GameInput.Instance.OnInteractAction += GameInput_OnInteractAction;
    53.     }
    54.  
    55.     public override void OnNetworkSpawn() {
    56.         state.OnValueChanged += State_OnValueChanged;
    57.         isGamePaused.OnValueChanged += IsGamePaused_OnValueChanged;
    58.  
    59.         if (IsServer) {
    60.             NetworkManager.Singleton.OnClientDisconnectCallback += NetworkManager_OnClientDisconnectCallback;
    61.             NetworkManager.Singleton.SceneManager.OnLoadEventCompleted += SceneManager_OnLoadEventCompleted;
    62.         }
    63.     }
    64.  
    65.     private void SceneManager_OnLoadEventCompleted(string sceneName, UnityEngine.SceneManagement.LoadSceneMode loadSceneMode, List<ulong> clientsCompleted, List<ulong> clientsTimedOut) {
    66.         foreach (ulong clientId in NetworkManager.Singleton.ConnectedClientsIds) {
    67.             Transform playerTransform = Instantiate(playerPrefab);
    68.             playerTransform.GetComponent<NetworkObject>().SpawnAsPlayerObject(clientId, true);
    69.         }
    70.     }
    71.  
    72.     private void NetworkManager_OnClientDisconnectCallback(ulong clientId) {
    73.         autoTestGamePausedState = true;
    74.     }
    75.  
    76.     private void IsGamePaused_OnValueChanged(bool previousValue, bool newValue) {
    77.         if (isGamePaused.Value) {
    78.             Time.timeScale = 0f;
    79.  
    80.             OnMultiplayerGamePaused?.Invoke(this, EventArgs.Empty);
    81.         } else {
    82.             Time.timeScale = 1f;
    83.  
    84.             OnMultiplayerGameUnPaused?.Invoke(this, EventArgs.Empty);
    85.         }
    86.     }
    87.  
    88.     private void State_OnValueChanged(State previousValue, State newValue) {
    89.         OnStateChanged?.Invoke(this, EventArgs.Empty);
    90.     }
    91.  
    92.     private void GameInput_OnInteractAction(object sender, EventArgs e) {
    93.         if (state.Value == State.WaitingToStart) {
    94.             isLocalPlayerReady = true;
    95.             OnLocalPlayerReadyChanged?.Invoke(this, EventArgs.Empty);
    96.  
    97.             SetPlayerReadyServerRpc();
    98.         }
    99.     }
    100.  
    101.     [ServerRpc(RequireOwnership = false)]
    102.     private void SetPlayerReadyServerRpc(ServerRpcParams serverRpcParams = default) {
    103.         playerReadyDictionary[serverRpcParams.Receive.SenderClientId] = true;
    104.  
    105.         bool allClientsReady = true;
    106.         foreach (ulong clientId in NetworkManager.Singleton.ConnectedClientsIds) {
    107.             if (!playerReadyDictionary.ContainsKey(clientId) || !playerReadyDictionary[clientId]) {
    108.                 // This player is NOT ready
    109.                 allClientsReady = false;
    110.                 break;
    111.             }
    112.         }
    113.  
    114.         if (allClientsReady) {
    115.             state.Value = State.CountDownToStart;
    116.         }
    117.     }
    118.  
    119.     private void GameInput_OnPauseAction(object sender, System.EventArgs e) {
    120.         TogglePauseGame();
    121.     }
    122.  
    123.     private void Update() {
    124.         if (!IsServer) {
    125.             return;
    126.         }
    127.  
    128.         switch (state.Value) {
    129.             case State.WaitingToStart:
    130.                 break;
    131.             case State.CountDownToStart:
    132.                 countdownToStartTimer.Value -= Time.deltaTime;
    133.                 if (countdownToStartTimer.Value < 0f) {
    134.                     state.Value = State.GamePlaying;
    135.                     gamePlayingTimer.Value = gamePlayingTimerMax;            
    136.                 }
    137.                 break;
    138.             case State.GamePlaying:
    139.                 gamePlayingTimer.Value -= Time.deltaTime;
    140.                 if (gamePlayingTimer.Value < 0f) {
    141.                     state.Value = State.GameOver;
    142.                 }
    143.                 break;
    144.             case State.GameOver:
    145.                 break;      
    146.         }
    147.     }
    148.  
    149.     private void LateUpdate() {
    150.         if (autoTestGamePausedState) {
    151.             autoTestGamePausedState = false;
    152.             TestGamePausedState();
    153.         }
    154.     }
    155.  
    156.     public bool IsGamePlaying() {
    157.         return state.Value == State.GamePlaying;
    158.     }
    159.  
    160.     public bool IsCountdownToStartActive() {
    161.         return state.Value == State.CountDownToStart;
    162.     }
    163.  
    164.     public float GetCountdownToStartTimer() {
    165.         return countdownToStartTimer.Value;
    166.     }
    167.  
    168.     public bool IsGameOver() {
    169.         return state.Value == State.GameOver;
    170.     }
    171.  
    172.     public bool IsWaitingToStart() {
    173.         return state.Value == State.WaitingToStart;
    174.     }
    175.  
    176.     public bool IsLocalPlayerReady() {
    177.         return isLocalPlayerReady;
    178.     }
    179.  
    180.     public float GetGamePlayingTimerNormalized() {
    181.         return 1 - (gamePlayingTimer.Value / gamePlayingTimerMax);
    182.     }
    183.  
    184.     public void TogglePauseGame() {
    185.         isLocalGamePaused = !isLocalGamePaused;
    186.         if (isLocalGamePaused) {
    187.             PauseGameServerRpc();
    188.  
    189.             OnLocalGamePaused?.Invoke(this, EventArgs.Empty);
    190.         }
    191.         else {
    192.             UnPauseGameServerRpc();
    193.  
    194.             OnLocalGameUnPaused?.Invoke(this, EventArgs.Empty);
    195.         }
    196.     }
    197.  
    198.     [ServerRpc(RequireOwnership = false)]
    199.     private void PauseGameServerRpc(ServerRpcParams serverRpcParams = default) {
    200.         playerPausedDictionary[serverRpcParams.Receive.SenderClientId] = true;
    201.  
    202.         TestGamePausedState();
    203.     }
    204.  
    205.     [ServerRpc(RequireOwnership = false)]
    206.     private void UnPauseGameServerRpc(ServerRpcParams serverRpcParams = default) {
    207.         playerPausedDictionary[serverRpcParams.Receive.SenderClientId] = false;
    208.  
    209.         TestGamePausedState();
    210.     }
    211.  
    212.     private void TestGamePausedState() {
    213.         foreach (ulong clientId in NetworkManager.Singleton.ConnectedClientsIds) {
    214.             if (playerPausedDictionary.ContainsKey(clientId) && playerPausedDictionary[clientId]) {
    215.                 // This player is paused
    216.                 isGamePaused.Value = true;
    217.                 return;
    218.             }
    219.         }
    220.  
    221.         // All players are unpaused
    222.         isGamePaused.Value = false;
    223.     }
    224.  
    225. }
    226.  


    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.InputSystem;
    6.  
    7. public class GameInput : MonoBehaviour {
    8.  
    9.     private const string PLAYER_PREFS_BINDINGS = "InputBindings";
    10.  
    11.     public static GameInput Instance { get; private set; }
    12.  
    13.     public event EventHandler OnInteractAction;
    14.     public event EventHandler OnInteractAlternateAction;
    15.     public event EventHandler OnPauseAction;
    16.     public event EventHandler OnBindingRebind;
    17.  
    18.     public enum Binding {
    19.         Move_Up,
    20.         Move_Down,
    21.         Move_Left,
    22.         Move_Right,
    23.         Interact,
    24.         InteractAlternate,
    25.         Pause,
    26.     }
    27.  
    28.     private PlayerInputActions playerInputActions;
    29.  
    30.  
    31.     private void Awake()
    32.     {
    33.         Instance = this;
    34.  
    35.         playerInputActions = new PlayerInputActions();
    36.        
    37.         if (PlayerPrefs.HasKey(PLAYER_PREFS_BINDINGS)) {
    38.             playerInputActions.LoadBindingOverridesFromJson(PlayerPrefs.GetString(PLAYER_PREFS_BINDINGS));
    39.         }
    40.         playerInputActions.Player.Enable();
    41.        
    42.         playerInputActions.Player.Interact.performed += Interact_performed;
    43.         playerInputActions.Player.InteractAlternate.performed += InteractAlternate_performed;
    44.         playerInputActions.Player.Pause.performed += Pause_performed;
    45.  
    46.     }
    47.  
    48.     private void OnDetsroy() {
    49.         playerInputActions.Player.Interact.performed -= Interact_performed;
    50.         playerInputActions.Player.InteractAlternate.performed -= InteractAlternate_performed;
    51.         playerInputActions.Player.Pause.performed -= Pause_performed;
    52.  
    53.         playerInputActions.Dispose();
    54.     }
    55.  
    56.     private void Pause_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj) {
    57.         OnPauseAction?.Invoke(this, EventArgs.Empty);
    58.     }
    59.  
    60.     private void InteractAlternate_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
    61.     {
    62.         OnInteractAlternateAction?.Invoke(this, EventArgs.Empty);
    63.     }
    64.  
    65.     private void Interact_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
    66.     {
    67.         OnInteractAction?.Invoke(this, EventArgs.Empty);
    68.     }
    69.  
    70.     public Vector2 GetMovementVectorNormalized()
    71.     {
    72.         Vector2 inputVector = playerInputActions.Player.Move.ReadValue<Vector2>();
    73.         inputVector = inputVector.normalized;
    74.         return inputVector;
    75.     }
    76.  
    77.     public string GetBindingText(Binding binding) {
    78.         switch (binding) {
    79.             default:
    80.             case Binding.Move_Up:
    81.                 return playerInputActions.Player.Move.bindings[1].ToDisplayString();
    82.             case Binding.Move_Down:
    83.                 return playerInputActions.Player.Move.bindings[2].ToDisplayString();
    84.             case Binding.Move_Left:
    85.                 return playerInputActions.Player.Move.bindings[3].ToDisplayString();
    86.             case Binding.Move_Right:
    87.                 return playerInputActions.Player.Move.bindings[4].ToDisplayString();
    88.             case Binding.Interact:
    89.                 return playerInputActions.Player.Interact.bindings[0].ToDisplayString();
    90.             case Binding.InteractAlternate:
    91.                 return playerInputActions.Player.InteractAlternate.bindings[0].ToDisplayString();
    92.             case Binding.Pause:
    93.                 return playerInputActions.Player.Pause.bindings[0].ToDisplayString();
    94.         }
    95.     }
    96.  
    97.     public void RebindBinding(Binding binding, Action OnActionRebound) {
    98.         playerInputActions.Player.Disable();
    99.  
    100.         InputAction inputAction;
    101.         int bindingIndex;
    102.  
    103.         switch (binding) {
    104.             default:
    105.             case Binding.Move_Up:
    106.                 inputAction = playerInputActions.Player.Move;
    107.                 bindingIndex = 1;
    108.                 break;
    109.             case Binding.Move_Down:
    110.                 inputAction = playerInputActions.Player.Move;
    111.                 bindingIndex = 2;
    112.                 break;
    113.             case Binding.Move_Left:
    114.                 inputAction =playerInputActions.Player.Move;
    115.                 bindingIndex = 3;
    116.                 break;
    117.             case Binding.Move_Right:
    118.                 inputAction = playerInputActions.Player.Move;
    119.                 bindingIndex = 4;
    120.                 break;
    121.             case Binding.Interact:
    122.                 inputAction = playerInputActions.Player.Interact;
    123.                 bindingIndex = 0;
    124.                 break;
    125.             case Binding.InteractAlternate:
    126.                 inputAction = playerInputActions.Player.InteractAlternate;
    127.                 bindingIndex = 0;
    128.                 break;
    129.             case Binding.Pause:
    130.                 inputAction = playerInputActions.Player.Pause;
    131.                 bindingIndex = 0;
    132.                 break;
    133.         }
    134.  
    135.         inputAction.PerformInteractiveRebinding(bindingIndex)
    136.             .OnComplete(callback => {
    137.                 callback.Dispose();
    138.                 playerInputActions.Player.Enable();
    139.                 OnActionRebound();
    140.  
    141.                 PlayerPrefs.SetString(PLAYER_PREFS_BINDINGS, playerInputActions.SaveBindingOverridesAsJson());
    142.                 PlayerPrefs.Save();
    143.  
    144.                 OnBindingRebind?.Invoke(this, EventArgs.Empty);
    145.         })
    146.         .Start();
    147. }
    148. }
    Please help
     
  2. frankluong

    frankluong

    Unity Technologies

    Joined:
    Feb 25, 2022
    Posts:
    2
    Hi,

    The version of NGO you are currently using is outdated (NGO 1.2.0). To ensure you have the best experience and access to the latest features and improvements, it is recommended to the latest NGO version (NGO 1.7.0), which is supported on Unity 2021.3 or later.

    Furthermore, do you have com.unity.multiplayer.tools installed and if so, what version are you using?
     
  3. yerimseni_343

    yerimseni_343

    Joined:
    Apr 15, 2023
    Posts:
    5
    I am currently using version 2022.2.16f1, I don't know why but NGO shows 1.3.1 as the latest version. Or is there another way to add the latest version? And yes multiplayer tools installed version 1.1.1