diff --git a/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs b/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs index e9c4fe4693..f15eec1583 100644 --- a/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs +++ b/com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs @@ -3736,7 +3736,13 @@ private void Start() private void InitGhost() { - if (!NetworkManager.IsListening) + if (!HasGhost || !NetworkObjectBridge || GhostAdapter.IsPrefab()) + { + // Nothing to register + return; + } + + if (!NetworkManager || !NetworkManager.IsListening) { if (NetworkManager.LogLevel == LogLevel.Developer) { @@ -3745,12 +3751,6 @@ private void InitGhost() return; } - if (!HasGhost || !NetworkObjectBridge || GhostAdapter.IsPrefab()) - { - // Nothing to register - return; - } - // All instances with Ghosts are automatically registered if (NetworkManager.LogLevel == LogLevel.Developer) { diff --git a/com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs b/com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs index b04d512a98..724e045f78 100644 --- a/com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs +++ b/com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs @@ -939,6 +939,14 @@ internal NetworkObject GetNetworkObjectToSpawn(uint globalObjectIdHash, ulong ow /// the instance of the internal NetworkObject InstantiateNetworkPrefab([NotNull] GameObject networkPrefab, uint prefabGlobalObjectIdHash, Vector3? position, Quaternion? rotation) { +#if UNIFIED_NETCODE + // TODO-FixMe: NetCode.Netcode.Instance is a singleton and might cause issues + // assigning this. + if (networkPrefab.GetComponent().HasGhost) + { + NetCode.Netcode.Instance.m_ActiveWorld = NetworkManager.NetcodeWorld; + } +#endif var gameObject = Object.Instantiate(networkPrefab); var networkObject = gameObject.GetComponent(); if (networkObject == null) diff --git a/com.unity.netcode.gameobjects/Tests/Runtime/TestHelpers/NetcodeIntegrationTest.cs b/com.unity.netcode.gameobjects/Tests/Runtime/TestHelpers/NetcodeIntegrationTest.cs index 82abbe2fd2..7153c5849a 100644 --- a/com.unity.netcode.gameobjects/Tests/Runtime/TestHelpers/NetcodeIntegrationTest.cs +++ b/com.unity.netcode.gameobjects/Tests/Runtime/TestHelpers/NetcodeIntegrationTest.cs @@ -2513,6 +2513,14 @@ protected void SpawnObjectInstance(NetworkObject networkObjectToSpawn, NetworkMa /// GameObject instance spawned private GameObject SpawnObject(NetworkObject prefabNetworkObject, NetworkManager owner, bool destroyWithScene = false, bool isPlayerObject = false) { +#if UNIFIED_NETCODE + // TODO-FixMe: NetCode.Netcode.Instance is a singleton and might cause issues + // assigning this. + if (prefabNetworkObject.HasGhost) + { + NetCode.Netcode.Instance.m_ActiveWorld = owner.NetcodeWorld; + } +#endif Assert.IsTrue(prefabNetworkObject.GlobalObjectIdHash > 0, $"{nameof(GameObject)} {prefabNetworkObject.name} has a {nameof(NetworkObject.GlobalObjectIdHash)} value of 0! Make sure to make it a valid prefab before trying to spawn!"); var newInstance = Object.Instantiate(prefabNetworkObject.gameObject); var networkObjectToSpawn = newInstance.GetComponent(); diff --git a/testproject/Assets/Tests/Runtime/Support/NetworkObjectSpawnerForTests.cs b/testproject/Assets/Tests/Runtime/Support/NetworkObjectSpawnerForTests.cs index f4102366fc..696304e67e 100644 --- a/testproject/Assets/Tests/Runtime/Support/NetworkObjectSpawnerForTests.cs +++ b/testproject/Assets/Tests/Runtime/Support/NetworkObjectSpawnerForTests.cs @@ -1,6 +1,9 @@ using System.Collections.Generic; using Unity.Netcode; using UnityEngine; +#if UNIFIED_NETCODE +using Unity.NetCode; +#endif namespace TestProject.RuntimeTests { @@ -75,6 +78,14 @@ private void SpawnRpc(RpcParams rpcParams = default) private void Spawn(ulong ownerId) { +#if UNIFIED_NETCODE + // TODO-FixMe: NetCode.Netcode.Instance is a singleton and might cause issues + // assigning this. + if (ObjectToSpawn.GetComponent().HasGhost) + { + Netcode.Instance.m_ActiveWorld = NetworkManager.NetcodeWorld; + } +#endif var instance = Instantiate(ObjectToSpawn); var networkObject = instance.GetComponent(); networkObject.SpawnWithOwnership(ownerId); diff --git a/testproject/Assets/Tests/Runtime/TestProject.Runtime.Tests.asmdef b/testproject/Assets/Tests/Runtime/TestProject.Runtime.Tests.asmdef index 51b61982a5..074cca9b5a 100644 --- a/testproject/Assets/Tests/Runtime/TestProject.Runtime.Tests.asmdef +++ b/testproject/Assets/Tests/Runtime/TestProject.Runtime.Tests.asmdef @@ -15,7 +15,8 @@ "Unity.Mathematics", "UnityEngine.TestRunner", "UnityEditor.TestRunner", - "Unity.Netcode.Runtime.Tests" + "Unity.Netcode.Runtime.Tests", + "Unity.NetCode" ], "includePlatforms": [], "excludePlatforms": [],