Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions com.unity.multiplayer.mlapi/Runtime/Core/NetworkBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,9 @@ public NetworkObject NetworkObject
m_NetworkObject = GetComponentInParent<NetworkObject>();
}

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;
Expand All @@ -312,18 +312,7 @@ public NetworkObject NetworkObject
/// <summary>
/// Gets whether or not this NetworkBehaviour instance has a NetworkObject owner.
/// </summary>
public bool HasNetworkObject
{
get
{
if (m_NetworkObject == null)
{
m_NetworkObject = GetComponentInParent<NetworkObject>();
}

return m_NetworkObject != null;
}
}
public bool HasNetworkObject => NetworkObject != null;

private NetworkObject m_NetworkObject = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,15 @@ public void AccessNetworkObjectTest()
var gameObject = new GameObject(nameof(AccessNetworkObjectTest));
var networkBehaviour = gameObject.AddComponent<EmptyNetworkBehaviour>();

// TODO: Do we really want to throw here?
// Future API change: return null
Assert.Throws<NullReferenceException>(() =>
{
var x = networkBehaviour.NetworkObject;
});
Assert.That(networkBehaviour.NetworkObject, Is.Null);

var networkObject = gameObject.AddComponent<NetworkObject>();

Assert.That(networkBehaviour.NetworkObject, Is.EqualTo(networkObject));

Object.DestroyImmediate(networkObject);

// TODO: Do we really want to throw here?
// Future API change: return null
Assert.Throws<NullReferenceException>(() =>
{
var x = networkBehaviour.NetworkObject;
});
Assert.That(networkBehaviour.NetworkObject, Is.Null);

// Cleanup
Object.DestroyImmediate(gameObject);
Expand Down