Skip to content

Commit efa756c

Browse files
committed
Handle null response from Instatiate call with data
1 parent e944ede commit efa756c

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

com.unity.netcode.gameobjects/Runtime/Spawning/NetworkPrefabHandler.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -283,23 +283,22 @@ internal uint GetSourceGlobalObjectIdHash(uint networkPrefabHash)
283283
internal NetworkObject HandleNetworkPrefabSpawn(uint networkPrefabAssetHash, ulong ownerClientId, Vector3 position, Quaternion rotation, byte[] instantiationData = null)
284284
{
285285
NetworkObject networkObjectInstance = null;
286+
var needsInstantiation = true;
286287
if (instantiationData != null)
287288
{
288289
if (m_PrefabAssetToPrefabHandlerWithData.TryGetValue(networkPrefabAssetHash, out var prefabInstanceHandler))
289290
{
291+
needsInstantiation = false;
290292
networkObjectInstance = prefabInstanceHandler.Instantiate(ownerClientId, position, rotation, instantiationData);
291293
}
292294
else
293295
{
294-
if (NetworkManager.Singleton.LogLevel <= LogLevel.Developer)
295-
{
296-
Debug.LogWarning($"[InstantiationData] Failed instantiate with data: no compatible data handler found for object hash {networkPrefabAssetHash}. Instantiation data will be dropped.");
297-
}
296+
Debug.LogError($"[InstantiationData] Failed instantiate with data: no compatible data handler found for object hash {networkPrefabAssetHash}. Instantiation data will be dropped.");
298297
}
299298
}
300299

301300
// Fallback to default handler
302-
if (!networkObjectInstance)
301+
if (needsInstantiation)
303302
{
304303
if (m_PrefabAssetToPrefabHandler.TryGetValue(networkPrefabAssetHash, out var prefabInstanceHandler))
305304
{
@@ -309,9 +308,9 @@ internal NetworkObject HandleNetworkPrefabSpawn(uint networkPrefabAssetHash, ulo
309308

310309
// Now we must make sure this alternate PrefabAsset spawned in place of the prefab asset with the networkPrefabAssetHash (GlobalObjectIdHash)
311310
// is registered and linked to the networkPrefabAssetHash so during the HandleNetworkPrefabDestroy process we can identify the alternate prefab asset.
312-
if (networkObjectInstance != null && !m_PrefabInstanceToPrefabAsset.ContainsKey(networkObjectInstance.GlobalObjectIdHash))
311+
if (networkObjectInstance != null)
313312
{
314-
m_PrefabInstanceToPrefabAsset.Add(networkObjectInstance.GlobalObjectIdHash, networkPrefabAssetHash);
313+
m_PrefabInstanceToPrefabAsset.TryAdd(networkObjectInstance.GlobalObjectIdHash, networkPrefabAssetHash);
315314
}
316315
return networkObjectInstance;
317316
}

com.unity.netcode.gameobjects/Runtime/Spawning/NetworkPrefabInstanceHandlerWithData.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ NetworkObject INetworkPrefabInstanceHandlerWithData.Instantiate(ulong ownerClien
3131
reader.ReadValueSafe(out T payload);
3232

3333
var networkObject = Instantiate(ownerClientId, position, rotation, payload);
34-
networkObject.InstantiationData = instantiationData;
34+
35+
if (networkObject != null)
36+
{
37+
networkObject.InstantiationData = instantiationData;
38+
}
3539

3640
return networkObject;
3741
}

0 commit comments

Comments
 (0)