Skip to content

Commit 70e0a2f

Browse files
authored
fix: #2147. Removing extra warning on despawning NetworkObjects with dirty NetworkVariables (#2151)
1 parent cec81a3 commit 70e0a2f

3 files changed

Lines changed: 36 additions & 8 deletions

File tree

com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ public void SpawnAsPlayerObject(ulong clientId, bool destroyWithScene = false)
509509
/// <param name="destroy">(true) the <see cref="GameObject"/> will be destroyed (false) the <see cref="GameObject"/> will persist after being despawned</param>
510510
public void Despawn(bool destroy = true)
511511
{
512+
MarkVariablesDirty(false);
512513
NetworkManager.SpawnManager.DespawnObject(this, destroy);
513514
}
514515

com.unity.netcode.gameobjects/Runtime/NetworkVariable/NetworkVariableBase.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,14 @@ public virtual void SetDirty(bool isDirty)
8181
{
8282
m_IsDirty = isDirty;
8383

84-
if (m_NetworkBehaviour == null)
85-
{
86-
Debug.LogWarning($"NetworkVariable is written to, but doesn't know its NetworkBehaviour yet. " +
87-
"Are you modifying a NetworkVariable before the NetworkObject is spawned?");
88-
return;
89-
}
90-
9184
if (m_IsDirty)
9285
{
86+
if (m_NetworkBehaviour == null)
87+
{
88+
Debug.LogWarning($"NetworkVariable is written to, but doesn't know its NetworkBehaviour yet. " +
89+
"Are you modifying a NetworkVariable before the NetworkObject is spawned?");
90+
return;
91+
}
9392
m_NetworkBehaviour.NetworkManager.MarkNetworkObjectDirty(m_NetworkBehaviour.NetworkObject);
9493
}
9594
}

com.unity.netcode.gameobjects/Tests/Runtime/NetworkShowHideTests.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class ShowHideObject : NetworkBehaviour
1717
{
1818
public static List<ShowHideObject> ClientTargetedNetworkObjects = new List<ShowHideObject>();
1919
public static ulong ClientIdToTarget;
20+
public static bool Silent;
2021

2122
public static NetworkObject GetNetworkObjectById(ulong networkObjectId)
2223
{
@@ -58,7 +59,10 @@ private void Awake()
5859

5960
public void Changed(int before, int after)
6061
{
61-
Debug.Log($"Value changed from {before} to {after}");
62+
if (!Silent)
63+
{
64+
Debug.Log($"Value changed from {before} to {after}");
65+
}
6266
}
6367
}
6468

@@ -264,5 +268,29 @@ public IEnumerator NetworkShowHideQuickTest()
264268
yield return CheckVisible(true);
265269
}
266270
}
271+
272+
[UnityTest]
273+
public IEnumerator NetworkHideDespawnTest()
274+
{
275+
m_ClientId0 = m_ClientNetworkManagers[0].LocalClientId;
276+
ShowHideObject.ClientTargetedNetworkObjects.Clear();
277+
ShowHideObject.ClientIdToTarget = m_ClientId0;
278+
ShowHideObject.Silent = true;
279+
280+
var spawnedObject1 = SpawnObject(m_PrefabToSpawn, m_ServerNetworkManager);
281+
var spawnedObject2 = SpawnObject(m_PrefabToSpawn, m_ServerNetworkManager);
282+
var spawnedObject3 = SpawnObject(m_PrefabToSpawn, m_ServerNetworkManager);
283+
m_NetSpawnedObject1 = spawnedObject1.GetComponent<NetworkObject>();
284+
m_NetSpawnedObject2 = spawnedObject2.GetComponent<NetworkObject>();
285+
m_NetSpawnedObject3 = spawnedObject3.GetComponent<NetworkObject>();
286+
287+
m_NetSpawnedObject1.GetComponent<ShowHideObject>().MyNetworkVariable.Value++;
288+
m_NetSpawnedObject1.NetworkHide(m_ClientId0);
289+
m_NetSpawnedObject1.Despawn();
290+
291+
yield return NetcodeIntegrationTestHelpers.WaitForTicks(m_ServerNetworkManager, 5);
292+
293+
LogAssert.NoUnexpectedReceived();
294+
}
267295
}
268296
}

0 commit comments

Comments
 (0)