From b1feb94287b3fca43ce8313902e459e2f06a8b2b Mon Sep 17 00:00:00 2001 From: PitouGames Date: Sun, 30 Apr 2023 23:36:04 +0200 Subject: [PATCH] fix: avoid throwing exception when using NetworkList without a NetworkManager (#2539) --- com.unity.netcode.gameobjects/CHANGELOG.md | 1 + .../NetworkVariable/Collections/NetworkList.cs | 12 ++++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/com.unity.netcode.gameobjects/CHANGELOG.md b/com.unity.netcode.gameobjects/CHANGELOG.md index 2980a3d450..453e75d661 100644 --- a/com.unity.netcode.gameobjects/CHANGELOG.md +++ b/com.unity.netcode.gameobjects/CHANGELOG.md @@ -20,6 +20,7 @@ Additional documentation and release notes are available at [Multiplayer Documen - Making a `NetworkVariable` with an `INetworkSerializable` type that doesn't meet the `new()` constraint will now create a compile-time error instead of an editor crash (#2528) - Fixed the inspector throwing exceptions when attempting to render `NetworkVariable`s of enum types. (#2529) - Fixed an exception and error logging when two different objects are shown and hidden on the same frame (#2524) +- Fixed usage of `NetworkList` throwing exception when used without a NetworkManager in scene. (#2539) ## Changed diff --git a/com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkList.cs b/com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkList.cs index 3d03d44134..fe52f3f348 100644 --- a/com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkList.cs +++ b/com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkList.cs @@ -374,7 +374,7 @@ public IEnumerator GetEnumerator() public void Add(T item) { // check write permissions - if (!CanClientWrite(m_NetworkBehaviour.NetworkManager.LocalClientId)) + if (m_NetworkBehaviour && !CanClientWrite(m_NetworkBehaviour.NetworkManager.LocalClientId)) { throw new InvalidOperationException("Client is not allowed to write to this NetworkList"); } @@ -395,7 +395,7 @@ public void Add(T item) public void Clear() { // check write permissions - if (!CanClientWrite(m_NetworkBehaviour.NetworkManager.LocalClientId)) + if (m_NetworkBehaviour && !CanClientWrite(m_NetworkBehaviour.NetworkManager.LocalClientId)) { throw new InvalidOperationException("Client is not allowed to write to this NetworkList"); } @@ -421,7 +421,7 @@ public bool Contains(T item) public bool Remove(T item) { // check write permissions - if (!CanClientWrite(m_NetworkBehaviour.NetworkManager.LocalClientId)) + if (m_NetworkBehaviour && !CanClientWrite(m_NetworkBehaviour.NetworkManager.LocalClientId)) { throw new InvalidOperationException("Client is not allowed to write to this NetworkList"); } @@ -456,7 +456,7 @@ public int IndexOf(T item) public void Insert(int index, T item) { // check write permissions - if (!CanClientWrite(m_NetworkBehaviour.NetworkManager.LocalClientId)) + if (m_NetworkBehaviour && !CanClientWrite(m_NetworkBehaviour.NetworkManager.LocalClientId)) { throw new InvalidOperationException("Client is not allowed to write to this NetworkList"); } @@ -485,7 +485,7 @@ public void Insert(int index, T item) public void RemoveAt(int index) { // check write permissions - if (!CanClientWrite(m_NetworkBehaviour.NetworkManager.LocalClientId)) + if (m_NetworkBehaviour && !CanClientWrite(m_NetworkBehaviour.NetworkManager.LocalClientId)) { throw new InvalidOperationException("Client is not allowed to write to this NetworkList"); } @@ -508,7 +508,7 @@ public T this[int index] set { // check write permissions - if (!CanClientWrite(m_NetworkBehaviour.NetworkManager.LocalClientId)) + if (m_NetworkBehaviour && !CanClientWrite(m_NetworkBehaviour.NetworkManager.LocalClientId)) { throw new InvalidOperationException("Client is not allowed to write to this NetworkList"); }