Skip to content

Commit a4e7bab

Browse files
fix:
We need to initialize prefabs prior to checking if we have Ghosts and need to use UnifiedNetcodeTransport. Fixing the logging and handling for users parenting NetworkManager during runtime.
1 parent 453c8e2 commit a4e7bab

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,11 +1018,13 @@ internal bool NetworkManagerCheckForParent(bool ignoreNetworkManagerCache = fals
10181018
{
10191019
#if UNITY_EDITOR
10201020
var isParented = NetworkManagerHelper.NotifyUserOfNestedNetworkManager(this, ignoreNetworkManagerCache);
1021+
1022+
10211023
#else
1022-
var isParented = transform.root != transform;
1024+
var isParented = transform.parent != null;
10231025
if (isParented)
10241026
{
1025-
throw new Exception(GenerateNestedNetworkManagerMessage(transform));
1027+
Log.Error(new Context(LogLevel.Error, GenerateNestedNetworkManagerMessage(transform)));
10261028
}
10271029
#endif
10281030
return isParented;
@@ -1038,7 +1040,17 @@ internal static string GenerateNestedNetworkManagerMessage(Transform transform)
10381040
/// </summary>
10391041
private void OnTransformParentChanged()
10401042
{
1043+
#if UNITY_EDITOR
1044+
// During editor playmode, we log the message as a dialog box
1045+
// and that script sets the parent back to root/null.
10411046
NetworkManagerCheckForParent();
1047+
#else
1048+
if (NetworkManagerCheckForParent())
1049+
{
1050+
// During runtime, we log the message and set our parent back to root/null.
1051+
transform.parent = null;
1052+
}
1053+
#endif
10421054
}
10431055

10441056
/// <summary>
@@ -1227,6 +1239,7 @@ internal void Initialize(bool server)
12271239
RealTimeProvider = ComponentFactory.Create<IRealTimeProvider>(this);
12281240

12291241
#if UNIFIED_NETCODE && OUT_OF_BAND_RPC
1242+
NetworkConfig.InitializePrefabs();
12301243
if (NetworkConfig.Prefabs.HasGhostPrefabs)
12311244
{
12321245
NetworkConfig.NetworkTransport = gameObject.AddComponent<UnifiedNetcodeTransport>();
@@ -1279,8 +1292,9 @@ internal void Initialize(bool server)
12791292

12801293
BehaviourUpdater = new NetworkBehaviourUpdater();
12811294
BehaviourUpdater.Initialize(this);
1282-
1295+
#if !UNIFIED_NETCODE
12831296
NetworkConfig.InitializePrefabs();
1297+
#endif
12841298
PrefabHandler.RegisterPlayerPrefab();
12851299
#if UNITY_EDITOR
12861300
BeginNetworkSession();
@@ -1844,7 +1858,7 @@ internal void ShutdownInternal()
18441858

18451859
#if UNIFIED_NETCODE
18461860
// TODO-UNIFIED: Review and align on this being a way to handle knowing if the world should be created.
1847-
if (NetworkConfig.Prefabs.HasGhostPrefabs)
1861+
if (NetworkConfig != null && NetworkConfig.Prefabs != null && NetworkConfig.Prefabs.HasGhostPrefabs)
18481862
{
18491863
try
18501864
{

0 commit comments

Comments
 (0)