From db38639d0fbe0aa81aa8200fce89a4d337f283e6 Mon Sep 17 00:00:00 2001 From: Jeffrey Rainy Date: Fri, 14 Oct 2022 10:23:57 -0400 Subject: [PATCH] fix: more robust checks around NetworkObjects being null --- .../Runtime/Core/NetworkBehaviour.cs | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs b/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs index 7f95289319..c3d7245540 100644 --- a/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs +++ b/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs @@ -286,7 +286,18 @@ internal string GenerateObserverErrorMessage(ClientRpcParams clientRpcParams, ul /// Gets the NetworkManager that owns this NetworkBehaviour instance /// See note around `NetworkObject` for how there is a chicken / egg problem when we are not initialized /// - public NetworkManager NetworkManager => NetworkObject.NetworkManager; + public NetworkManager NetworkManager + { + get + { + if (NetworkObject?.NetworkManager != null) + { + return NetworkObject?.NetworkManager; + } + + return NetworkManager.Singleton; + } + } /// /// If a NetworkObject is assigned, it will return whether or not this NetworkObject @@ -349,9 +360,16 @@ public NetworkObject NetworkObject { get { - if (m_NetworkObject == null) + try + { + if (m_NetworkObject == null) + { + m_NetworkObject = GetComponentInParent(); + } + } + catch (Exception) { - m_NetworkObject = GetComponentInParent(); + return null; } // ShutdownInProgress check: