Skip to content

Commit 45cfa90

Browse files
jeffreyrainyNoelStephensUnity
authored andcommitted
fix: splitting the internal and visible parts of OnNetworkSpawn (Unity-Technologies#2143)
* fix: splitting the internal and visible parts of OnNetworkSpawn * fix: Not throwing an exception when NetworkVariable is written to before being spawned. Logging a warning instead Co-authored-by: Noel Stephens <noel.stephens@unity3d.com>
1 parent d75acc9 commit 45cfa90

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,10 @@ internal void InternalOnNetworkSpawn()
436436
IsSpawned = true;
437437
InitializeVariables();
438438
UpdateNetworkProperties();
439+
}
440+
441+
internal void VisibleOnNetworkSpawn()
442+
{
439443
OnNetworkSpawn();
440444
}
441445

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,13 @@ internal void InvokeBehaviourNetworkSpawn()
821821
Debug.LogWarning($"{ChildNetworkBehaviours[i].gameObject.name} is disabled! Netcode for GameObjects does not support spawning disabled NetworkBehaviours! The {ChildNetworkBehaviours[i].GetType().Name} component was skipped during spawn!");
822822
}
823823
}
824+
for (int i = 0; i < ChildNetworkBehaviours.Count; i++)
825+
{
826+
if (ChildNetworkBehaviours[i].gameObject.activeInHierarchy)
827+
{
828+
ChildNetworkBehaviours[i].VisibleOnNetworkSpawn();
829+
}
830+
}
824831
}
825832

826833
internal void InvokeBehaviourNetworkDespawn()

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using UnityEngine;
23

34
namespace Unity.Netcode
45
{
@@ -79,7 +80,15 @@ protected NetworkVariableBase(
7980
public virtual void SetDirty(bool isDirty)
8081
{
8182
m_IsDirty = isDirty;
82-
if (m_IsDirty && m_NetworkBehaviour != null)
83+
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+
91+
if (m_IsDirty)
8392
{
8493
m_NetworkBehaviour.NetworkManager.MarkNetworkObjectDirty(m_NetworkBehaviour.NetworkObject);
8594
}

0 commit comments

Comments
 (0)