diff --git a/com.unity.multiplayer.mlapi/Runtime/Core/NetworkBehaviour.cs b/com.unity.multiplayer.mlapi/Runtime/Core/NetworkBehaviour.cs index 36d7d56dd8..dfb8295d37 100644 --- a/com.unity.multiplayer.mlapi/Runtime/Core/NetworkBehaviour.cs +++ b/com.unity.multiplayer.mlapi/Runtime/Core/NetworkBehaviour.cs @@ -300,9 +300,9 @@ public NetworkObject NetworkObject m_NetworkObject = GetComponentInParent(); } - if (m_NetworkObject == null) + if (m_NetworkObject == null && NetworkLog.CurrentLogLevel <= LogLevel.Normal) { - throw new NullReferenceException($"Could not get {nameof(NetworkObject)} for the {nameof(NetworkBehaviour)}. Are you missing a {nameof(NetworkObject)} component?"); + NetworkLog.LogWarning($"Could not get {nameof(NetworkObject)} for the {nameof(NetworkBehaviour)}. Are you missing a {nameof(NetworkObject)} component?"); } return m_NetworkObject; @@ -312,18 +312,7 @@ public NetworkObject NetworkObject /// /// Gets whether or not this NetworkBehaviour instance has a NetworkObject owner. /// - public bool HasNetworkObject - { - get - { - if (m_NetworkObject == null) - { - m_NetworkObject = GetComponentInParent(); - } - - return m_NetworkObject != null; - } - } + public bool HasNetworkObject => NetworkObject != null; private NetworkObject m_NetworkObject = null; diff --git a/com.unity.multiplayer.mlapi/Tests/Editor/NetworkBehaviourTests.cs b/com.unity.multiplayer.mlapi/Tests/Editor/NetworkBehaviourTests.cs index b12870d70a..d5b5a34013 100644 --- a/com.unity.multiplayer.mlapi/Tests/Editor/NetworkBehaviourTests.cs +++ b/com.unity.multiplayer.mlapi/Tests/Editor/NetworkBehaviourTests.cs @@ -33,12 +33,7 @@ public void AccessNetworkObjectTest() var gameObject = new GameObject(nameof(AccessNetworkObjectTest)); var networkBehaviour = gameObject.AddComponent(); - // TODO: Do we really want to throw here? - // Future API change: return null - Assert.Throws(() => - { - var x = networkBehaviour.NetworkObject; - }); + Assert.That(networkBehaviour.NetworkObject, Is.Null); var networkObject = gameObject.AddComponent(); @@ -46,12 +41,7 @@ public void AccessNetworkObjectTest() Object.DestroyImmediate(networkObject); - // TODO: Do we really want to throw here? - // Future API change: return null - Assert.Throws(() => - { - var x = networkBehaviour.NetworkObject; - }); + Assert.That(networkBehaviour.NetworkObject, Is.Null); // Cleanup Object.DestroyImmediate(gameObject);