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.CallbackArray1[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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using Unity.Netcode;
using UnityEngine.SceneManagement;
public class KitchenGameManager : NetworkBehaviour
{
public static KitchenGameManager Instance { get; private set; }
public event EventHandler OnStateChanged;
public event EventHandler OnLocalGamePaused;
public event EventHandler OnLocalGameUnPaused;
public event EventHandler OnMultiplayerGamePaused;
public event EventHandler OnMultiplayerGameUnPaused;
public event EventHandler OnLocalPlayerReadyChanged;
private enum State {
WaitingToStart,
CountDownToStart,
GamePlaying,
GameOver,
}
[SerializeField] private Transform playerPrefab;
private NetworkVariable<State> state = new NetworkVariable<State>(State.WaitingToStart);
private bool isLocalPlayerReady;
private NetworkVariable<float> countdownToStartTimer = new NetworkVariable<float>(3f);
private NetworkVariable<float> gamePlayingTimer = new NetworkVariable<float>(0f);
private float gamePlayingTimerMax = 1000f;
private bool isLocalGamePaused = false;
private NetworkVariable<bool> isGamePaused = new NetworkVariable<bool>(false);
private Dictionary<ulong, bool> playerReadyDictionary;
private Dictionary<ulong, bool> playerPausedDictionary;
private bool autoTestGamePausedState;
public static bool interactionsEnabled = false;
private void Awake() {
Instance = this;
playerReadyDictionary = new Dictionary<ulong, bool>();
playerPausedDictionary = new Dictionary<ulong, bool>();
}
private void Start() {
GameInput.Instance.OnPauseAction += GameInput_OnPauseAction;
GameInput.Instance.OnInteractAction += GameInput_OnInteractAction;
}
public override void OnNetworkSpawn() {
state.OnValueChanged += State_OnValueChanged;
isGamePaused.OnValueChanged += IsGamePaused_OnValueChanged;
if (IsServer) {
NetworkManager.Singleton.OnClientDisconnectCallback += NetworkManager_OnClientDisconnectCallback;
NetworkManager.Singleton.SceneManager.OnLoadEventCompleted += SceneManager_OnLoadEventCompleted;
}
}
private void SceneManager_OnLoadEventCompleted(string sceneName, UnityEngine.SceneManagement.LoadSceneMode loadSceneMode, List<ulong> clientsCompleted, List<ulong> clientsTimedOut) {
foreach (ulong clientId in NetworkManager.Singleton.ConnectedClientsIds) {
Transform playerTransform = Instantiate(playerPrefab);
playerTransform.GetComponent<NetworkObject>().SpawnAsPlayerObject(clientId, true);
}
}
private void NetworkManager_OnClientDisconnectCallback(ulong clientId) {
autoTestGamePausedState = true;
}
private void IsGamePaused_OnValueChanged(bool previousValue, bool newValue) {
if (isGamePaused.Value) {
Time.timeScale = 0f;
OnMultiplayerGamePaused?.Invoke(this, EventArgs.Empty);
} else {
Time.timeScale = 1f;
OnMultiplayerGameUnPaused?.Invoke(this, EventArgs.Empty);
}
}
private void State_OnValueChanged(State previousValue, State newValue) {
OnStateChanged?.Invoke(this, EventArgs.Empty);
}
private void GameInput_OnInteractAction(object sender, EventArgs e) {
if (state.Value == State.WaitingToStart) {
isLocalPlayerReady = true;
OnLocalPlayerReadyChanged?.Invoke(this, EventArgs.Empty);
SetPlayerReadyServerRpc();
}
}
[ServerRpc(RequireOwnership = false)]
private void SetPlayerReadyServerRpc(ServerRpcParams serverRpcParams = default) {
playerReadyDictionary[serverRpcParams.Receive.SenderClientId] = true;
bool allClientsReady = true;
foreach (ulong clientId in NetworkManager.Singleton.ConnectedClientsIds) {
if (!playerReadyDictionary.ContainsKey(clientId) || !playerReadyDictionary[clientId]) {
// This player is NOT ready
allClientsReady = false;
break;
}
}
if (allClientsReady) {
state.Value = State.CountDownToStart;
}
}
private void GameInput_OnPauseAction(object sender, System.EventArgs e) {
TogglePauseGame();
}
private void Update() {
if (!IsServer) {
return;
}
switch (state.Value) {
case State.WaitingToStart:
break;
case State.CountDownToStart:
countdownToStartTimer.Value -= Time.deltaTime;
if (countdownToStartTimer.Value < 0f) {
state.Value = State.GamePlaying;
gamePlayingTimer.Value = gamePlayingTimerMax;
}
break;
case State.GamePlaying:
gamePlayingTimer.Value -= Time.deltaTime;
if (gamePlayingTimer.Value < 0f) {
state.Value = State.GameOver;
}
break;
case State.GameOver:
break;
}
}
private void LateUpdate() {
if (autoTestGamePausedState) {
autoTestGamePausedState = false;
TestGamePausedState();
}
}
public bool IsGamePlaying() {
return state.Value == State.GamePlaying;
}
public bool IsCountdownToStartActive() {
return state.Value == State.CountDownToStart;
}
public float GetCountdownToStartTimer() {
return countdownToStartTimer.Value;
}
public bool IsGameOver() {
return state.Value == State.GameOver;
}
public bool IsWaitingToStart() {
return state.Value == State.WaitingToStart;
}
public bool IsLocalPlayerReady() {
return isLocalPlayerReady;
}
public float GetGamePlayingTimerNormalized() {
return 1 - (gamePlayingTimer.Value / gamePlayingTimerMax);
}
public void TogglePauseGame() {
isLocalGamePaused = !isLocalGamePaused;
if (isLocalGamePaused) {
PauseGameServerRpc();
OnLocalGamePaused?.Invoke(this, EventArgs.Empty);
}
else {
UnPauseGameServerRpc();
OnLocalGameUnPaused?.Invoke(this, EventArgs.Empty);
}
}
[ServerRpc(RequireOwnership = false)]
private void PauseGameServerRpc(ServerRpcParams serverRpcParams = default) {
playerPausedDictionary[serverRpcParams.Receive.SenderClientId] = true;
TestGamePausedState();
}
[ServerRpc(RequireOwnership = false)]
private void UnPauseGameServerRpc(ServerRpcParams serverRpcParams = default) {
playerPausedDictionary[serverRpcParams.Receive.SenderClientId] = false;
TestGamePausedState();
}
private void TestGamePausedState() {
foreach (ulong clientId in NetworkManager.Singleton.ConnectedClientsIds) {
if (playerPausedDictionary.ContainsKey(clientId) && playerPausedDictionary[clientId]) {
// This player is paused
isGamePaused.Value = true;
return;
}
}
// All players are unpaused
isGamePaused.Value = false;
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class GameInput : MonoBehaviour {
private const string PLAYER_PREFS_BINDINGS = "InputBindings";
public static GameInput Instance { get; private set; }
public event EventHandler OnInteractAction;
public event EventHandler OnInteractAlternateAction;
public event EventHandler OnPauseAction;
public event EventHandler OnBindingRebind;
public enum Binding {
Move_Up,
Move_Down,
Move_Left,
Move_Right,
Interact,
InteractAlternate,
Pause,
}
private PlayerInputActions playerInputActions;
private void Awake()
{
Instance = this;
playerInputActions = new PlayerInputActions();
if (PlayerPrefs.HasKey(PLAYER_PREFS_BINDINGS)) {
playerInputActions.LoadBindingOverridesFromJson(PlayerPrefs.GetString(PLAYER_PREFS_BINDINGS));
}
playerInputActions.Player.Enable();
playerInputActions.Player.Interact.performed += Interact_performed;
playerInputActions.Player.InteractAlternate.performed += InteractAlternate_performed;
playerInputActions.Player.Pause.performed += Pause_performed;
}
private void OnDetsroy() {
playerInputActions.Player.Interact.performed -= Interact_performed;
playerInputActions.Player.InteractAlternate.performed -= InteractAlternate_performed;
playerInputActions.Player.Pause.performed -= Pause_performed;
playerInputActions.Dispose();
}
private void Pause_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj) {
OnPauseAction?.Invoke(this, EventArgs.Empty);
}
private void InteractAlternate_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
OnInteractAlternateAction?.Invoke(this, EventArgs.Empty);
}
private void Interact_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
OnInteractAction?.Invoke(this, EventArgs.Empty);
}
public Vector2 GetMovementVectorNormalized()
{
Vector2 inputVector = playerInputActions.Player.Move.ReadValue<Vector2>();
inputVector = inputVector.normalized;
return inputVector;
}
public string GetBindingText(Binding binding) {
switch (binding) {
default:
case Binding.Move_Up:
return playerInputActions.Player.Move.bindings[1].ToDisplayString();
case Binding.Move_Down:
return playerInputActions.Player.Move.bindings[2].ToDisplayString();
case Binding.Move_Left:
return playerInputActions.Player.Move.bindings[3].ToDisplayString();
case Binding.Move_Right:
return playerInputActions.Player.Move.bindings[4].ToDisplayString();
case Binding.Interact:
return playerInputActions.Player.Interact.bindings[0].ToDisplayString();
case Binding.InteractAlternate:
return playerInputActions.Player.InteractAlternate.bindings[0].ToDisplayString();
case Binding.Pause:
return playerInputActions.Player.Pause.bindings[0].ToDisplayString();
}
}
public void RebindBinding(Binding binding, Action OnActionRebound) {
playerInputActions.Player.Disable();
InputAction inputAction;
int bindingIndex;
switch (binding) {
default:
case Binding.Move_Up:
inputAction = playerInputActions.Player.Move;
bindingIndex = 1;
break;
case Binding.Move_Down:
inputAction = playerInputActions.Player.Move;
bindingIndex = 2;
break;
case Binding.Move_Left:
inputAction =playerInputActions.Player.Move;
bindingIndex = 3;
break;
case Binding.Move_Right:
inputAction = playerInputActions.Player.Move;
bindingIndex = 4;
break;
case Binding.Interact:
inputAction = playerInputActions.Player.Interact;
bindingIndex = 0;
break;
case Binding.InteractAlternate:
inputAction = playerInputActions.Player.InteractAlternate;
bindingIndex = 0;
break;
case Binding.Pause:
inputAction = playerInputActions.Player.Pause;
bindingIndex = 0;
break;
}
inputAction.PerformInteractiveRebinding(bindingIndex)
.OnComplete(callback => {
callback.Dispose();
playerInputActions.Player.Enable();
OnActionRebound();
PlayerPrefs.SetString(PLAYER_PREFS_BINDINGS, playerInputActions.SaveBindingOverridesAsJson());
PlayerPrefs.Save();
OnBindingRebind?.Invoke(this, EventArgs.Empty);
})
.Start();
}
}
Please help