Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: Avoid throwing exception when using NetworkList without a Networ…
…kManager [Up-port] (#3504)

<!-- Replace this block with what this PR does and why. Describe what
you'd like reviewers to know, how you applied the engineering
principles, and any interesting tradeoffs made. Delete bullet points
below that don't apply, and update the changelog section as appropriate.
-->

<!-- Add short version of the JIRA ticket to the PR title (e.g. "feat:
new shiny feature [MTT-123]") -->
Up-port of #3502  
Fixes #2539 


## Changelog

- Added: `LocalClientCannotWrite` function to co-locate the shared
functionality
- Fixed: Ensure that the `NetworkManager` exists before attempting to
access the `LocalClientId`

## Testing and Documentation

- No tests have been added.

<!-- Uncomment and mark items off with a * if this PR deprecates any
API:
### Deprecated API
- [ ] An `[Obsolete]` attribute was added along with a `(RemovedAfter
yyyy-mm-dd)` entry.
- [ ] An [api updater] was added.
- [ ] Deprecation of the API is explained in the CHANGELOG.
- [ ] The users can understand why this API was removed and what they
should use instead.
-->

## Backport

<!-- If this is a backport:
 - Add the following to the PR title: "\[Backport\] ..." .
 - Link to the original PR.
If this needs a backport - state this here
If a backport is not needed please provide the reason why.
If the "Backports" section is not present it will lead to a CI test
failure.
-->

Backported in #3502

---------

Co-authored-by: unity-renovate[bot] <120015202+unity-renovate[bot]@users.noreply.github.com>
Co-authored-by: Emma <emma.mcmillan@unity3d.com>
  • Loading branch information
3 people authored Jun 14, 2025
commit 7bbc1c3a950ed4c5c10f3be25bd19ae1900d5f52
1 change: 1 addition & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Additional documentation and release notes are available at [Multiplayer Documen

### Fixed

- Fixed `NullReferenceException` on `NetworkList` when used without a NetworkManager in scene. (#3503)
- Fixed issue where `NetworkClient` could persist some settings if re-using the same `NetworkManager` instance. (#3491)
- Fixed issue where a pooled `NetworkObject` was not resetting the internal latest parent property when despawned. (#3491)
- Fixed issue where the initial client synchronization pre-serialization process was not excluding spawned `NetworkObject` instances that already had pending visibility for the client being synchronized. (#3488)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ public void Serialize(FastBufferWriter writer, int targetVersion)
var startingSize = writer.Length;
var networkVariable = NetworkBehaviour.NetworkVariableFields[i];
var shouldWrite = networkVariable.IsDirty() &&
networkVariable.CanClientRead(TargetClientId) &&
(networkManager.IsServer || networkVariable.CanClientWrite(networkManager.LocalClientId)) &&
networkVariable.CanSend();
networkVariable.CanClientRead(TargetClientId)
&& (networkManager.IsServer ||
(networkVariable.CanWrite && networkVariable.CanSend()));

// Prevent the server from writing to the client that owns a given NetworkVariable
// Allowing the write would send an old value to the client and cause jitter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public void Anticipate(T value)
m_LastAnticipationCounter = m_NetworkBehaviour.NetworkManager.AnticipationSystem.AnticipationCounter;
m_AnticipatedValue = value;
NetworkVariableSerialization<T>.Duplicate(m_AnticipatedValue, ref m_PreviousAnticipatedValue);
if (CanClientWrite(m_NetworkBehaviour.NetworkManager.LocalClientId))
if (CanWrite)
{
AuthoritativeValue = value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ public IEnumerator<T> GetEnumerator()
public void Add(T item)
{
// check write permissions
if (!CanClientWrite(m_NetworkManager.LocalClientId))
if (CannotWrite)
{
LogWritePermissionError();
return;
Expand All @@ -455,7 +455,7 @@ public void Add(T item)
public void Clear()
{
// check write permissions
if (!CanClientWrite(m_NetworkManager.LocalClientId))
if (CannotWrite)
{
LogWritePermissionError();
return;
Expand Down Expand Up @@ -493,7 +493,7 @@ public bool Contains(T item)
public bool Remove(T item)
{
// check write permissions
if (!CanClientWrite(m_NetworkManager.LocalClientId))
if (CannotWrite)
{
LogWritePermissionError();
return false;
Expand Down Expand Up @@ -542,7 +542,7 @@ public int IndexOf(T item)
public void Insert(int index, T item)
{
// check write permissions
if (!CanClientWrite(m_NetworkManager.LocalClientId))
if (CannotWrite)
{
LogWritePermissionError();
return;
Expand Down Expand Up @@ -578,7 +578,7 @@ public void Insert(int index, T item)
public void RemoveAt(int index)
{
// check write permissions
if (!CanClientWrite(m_NetworkManager.LocalClientId))
if (CannotWrite)
{
throw new InvalidOperationException("Client is not allowed to write to this NetworkList");
}
Expand Down Expand Up @@ -610,7 +610,7 @@ public T this[int index]
set
{
// check write permissions
if (!CanClientWrite(m_NetworkManager.LocalClientId))
if (CannotWrite)
{
LogWritePermissionError();
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public virtual T Value
get => m_InternalValue;
set
{
if (m_NetworkManager && !CanClientWrite(m_NetworkManager.LocalClientId))
if (CannotWrite)
{
LogWritePermissionError();
return;
Expand Down Expand Up @@ -162,7 +162,7 @@ public bool CheckDirtyState(bool forceCheck = false)
var isDirty = base.IsDirty();

// A client without permissions invoking this method should only check to assure the current value is equal to the last known current value
if (m_NetworkManager && !CanClientWrite(m_NetworkManager.LocalClientId))
if (CannotWrite)
{
// If modifications are detected, then revert back to the last known current value
if (!NetworkVariableSerialization<T>.AreEqual(ref m_InternalValue, ref m_InternalOriginalValue))
Expand Down Expand Up @@ -245,7 +245,7 @@ public override bool IsDirty()
{
// If the client does not have write permissions but the internal value is determined to be locally modified and we are applying updates, then we should revert
// to the original collection value prior to applying updates (primarily for collections).
if (!NetworkUpdaterCheck && m_NetworkManager && !CanClientWrite(m_NetworkManager.LocalClientId) && !NetworkVariableSerialization<T>.AreEqual(ref m_InternalValue, ref m_InternalOriginalValue))
if (!NetworkUpdaterCheck && CannotWrite && !NetworkVariableSerialization<T>.AreEqual(ref m_InternalValue, ref m_InternalOriginalValue))
{
NetworkVariableSerialization<T>.Duplicate(m_InternalOriginalValue, ref m_InternalValue);
return true;
Expand Down Expand Up @@ -307,7 +307,7 @@ public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
{
// If the client does not have write permissions but the internal value is determined to be locally modified and we are applying updates, then we should revert
// to the original collection value prior to applying updates (primarily for collections).
if (m_NetworkManager && !CanClientWrite(m_NetworkManager.LocalClientId) && !NetworkVariableSerialization<T>.AreEqual(ref m_InternalOriginalValue, ref m_InternalValue))
if (CannotWrite && !NetworkVariableSerialization<T>.AreEqual(ref m_InternalOriginalValue, ref m_InternalValue))
{
NetworkVariableSerialization<T>.Duplicate(m_InternalOriginalValue, ref m_InternalValue);
}
Expand Down Expand Up @@ -349,7 +349,7 @@ public override void ReadField(FastBufferReader reader)
{
// If the client does not have write permissions but the internal value is determined to be locally modified and we are applying updates, then we should revert
// to the original collection value prior to applying updates (primarily for collections).
if (m_NetworkManager && !CanClientWrite(m_NetworkManager.LocalClientId) && !NetworkVariableSerialization<T>.AreEqual(ref m_InternalOriginalValue, ref m_InternalValue))
if (CannotWrite && !NetworkVariableSerialization<T>.AreEqual(ref m_InternalOriginalValue, ref m_InternalValue))
{
NetworkVariableSerialization<T>.Duplicate(m_InternalOriginalValue, ref m_InternalValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,16 @@ public bool CanClientWrite(ulong clientId)
}
}

/// <summary>
/// Returns true if the current <see cref="NetworkManager.LocalClientId"/> can write to this variable; otherwise false.
/// </summary>
internal bool CanWrite => m_NetworkManager && CanClientWrite(m_NetworkManager.LocalClientId);

/// <summary>
/// Returns false if the current <see cref="NetworkManager.LocalClientId"/> can write to this variable; otherwise true.
/// </summary>
internal bool CannotWrite => m_NetworkManager && !CanClientWrite(m_NetworkManager.LocalClientId);

/// <summary>
/// Returns the ClientId of the owning client
/// </summary>
Expand Down