I’m working on a multiplayer game in Unity using Netcode for GameObjects, and I’ve encountered an issue when dropping items from the player’s inventory. When the player drops an item, the newly spawned item in the world receives a different NetworkObjectId (or HashID), even though it’s the same item that was in the inventory.
What I’m doing:
I’m using Netcode for GameObjects to synchronize item interactions between clients and the server.
Each item in the inventory has a unique ID, which I generate when the item is first created, so that I can track it.
When a player picks up an item, it gets added to their inventory, and when they drop it, the item is instantiated again in the world.
The problem occurs during the drop: the newly spawned item gets a new NetworkObjectId, even though I need it to retain the same identifier it had while it was in the inventory.
PlayerInteract.cs
using UnityEngine;
using Unity.Netcode;
using TMPro;
public class PlayerInteract : NetworkBehaviour
{
public float interactionRange = 3f;
public Camera playerCamera;
public GameObject interactUI;
private TMP_Text interactText;
private Item itemInFocus;
private PlayerInventory playerInventory;
private void Start()
{
if (interactUI != null)
{
interactText = interactUI.GetComponent<TMP_Text>();
}
playerInventory = GetComponent<PlayerInventory>();
}
private void Update()
{
if (!IsOwner) return;
HandleItemInteraction();
if (Input.GetKeyDown(KeyCode.G))
{
playerInventory.DropCurrentItemServerRpc();
}
if (Input.mouseScrollDelta.y > 0)
{
playerInventory.SwitchItem(1);
}
else if (Input.mouseScrollDelta.y < 0)
{
playerInventory.SwitchItem(-1);
}
}
private void HandleItemInteraction()
{
Ray ray = playerCamera.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2));
if (Physics.Raycast(ray, out RaycastHit hit, interactionRange))
{
Item item = hit.collider.GetComponent<Item>();
if (item != null)
{
itemInFocus = item;
ShowItemInfo(item);
if (Input.GetKeyDown(KeyCode.E))
{
PickupItemServerRpc(item.NetworkObjectId);
}
}
else
{
HideItemInfo();
}
}
else
{
HideItemInfo();
}
}
void ShowItemInfo(Item item)
{
if (interactText != null)
{
interactUI.SetActive(true);
interactText.text = $"Нажмите E, чтобы подобрать {item.itemName}";
}
}
void HideItemInfo()
{
if (interactUI != null)
{
interactUI.SetActive(false);
}
}
[ServerRpc(RequireOwnership = false)]
void PickupItemServerRpc(ulong itemId, ServerRpcParams rpcParams = default)
{
if (NetworkManager.Singleton.SpawnManager.SpawnedObjects.TryGetValue(itemId, out NetworkObject networkObject))
{
Item item = networkObject.GetComponent<Item>();
if (item != null)
{
playerInventory.AddItemToInventoryServerRpc(item.itemName, itemId);
}
}
}
}
PlayerInventory.cs
using System.Collections.Generic;
using UnityEngine;
using Unity.Netcode;
public class PlayerInventory : NetworkBehaviour
{
public List<GameObject> allItems;
public List<GameObject> collectedItems = new List<GameObject>();
private int currentItemIndex = -1;
private const int maxInventorySlots = 3;
public Transform dropPosition;
public override void OnNetworkSpawn()
{
if (!IsServer) return;
foreach (GameObject item in allItems)
{
item.SetActive(false);
HideAllItemsForAllClientsClientRpc();
}
}
[ServerRpc(RequireOwnership = false)]
public void AddItemToInventoryServerRpc(string itemName, ulong networkObjectId)
{
if (collectedItems.Count < maxInventorySlots)
{
GameObject itemToActivate = allItems.Find(item => item.name == itemName);
if (itemToActivate != null)
{
collectedItems.Add(itemToActivate);
RemoveItemFromWorldServerRpc(networkObjectId);
currentItemIndex = collectedItems.Count - 1;
ActivateItemForAllClientsClientRpc(currentItemIndex);
SyncItemsForAllClientsClientRpc();
}
}
else
{
Debug.Log("Inventory is full!");
}
}
[ServerRpc(RequireOwnership = false)]
public void RemoveItemFromWorldServerRpc(ulong networkObjectId)
{
if (NetworkManager.Singleton.SpawnManager.SpawnedObjects.TryGetValue(networkObjectId, out NetworkObject networkObject))
{
networkObject.Despawn(true);
}
}
[ClientRpc]
void HideAllItemsForAllClientsClientRpc()
{
foreach (var item in allItems)
{
item.SetActive(false);
}
}
[ClientRpc]
void ActivateItemForAllClientsClientRpc(int index)
{
HideAllItemsForAllClientsClientRpc();
if (index >= 0 && index < collectedItems.Count)
{
collectedItems[index].SetActive(true);
}
}
[ServerRpc(RequireOwnership = false)]
public void DropCurrentItemServerRpc()
{
if (currentItemIndex >= 0 && currentItemIndex < collectedItems.Count)
{
GameObject currentItem = collectedItems[currentItemIndex];
if (currentItem == null) return;
// Получаем компонент Item, чтобы использовать его префаб
Item itemComponent = currentItem.GetComponent<Item>();
if (itemComponent == null || itemComponent.itemPrefab == null)
{
Debug.LogError("Компонент Item или префаб не найден на предмете!");
return;
}
// Спавним предмет на сервере используя префаб из компонента Item
GameObject droppedItem = Instantiate(itemComponent.itemPrefab, dropPosition.position, Quaternion.identity);
NetworkObject networkObject = droppedItem.GetComponent<NetworkObject>() ?? droppedItem.AddComponent<NetworkObject>();
// Спавн с синхронизацией по сети для всех клиентов
networkObject.Spawn(true);
// Добавляем физику для выбрасывания предмета
Rigidbody rb = droppedItem.GetComponent<Rigidbody>() ?? droppedItem.AddComponent<Rigidbody>();
rb.AddForce(dropPosition.forward * 1.5f, ForceMode.Impulse);
// Деактивируем текущий предмет в инвентаре
currentItem.SetActive(false);
collectedItems.RemoveAt(currentItemIndex);
// Обновляем индекс текущего предмета
UpdateCurrentItemIndex();
// Уведомляем клиентов о выбрасывании предмета
NotifyItemDroppedForAllClientsClientRpc(droppedItem.name, droppedItem.GetComponent<NetworkObject>().NetworkObjectId, droppedItem.transform.position, droppedItem.transform.rotation);
}
}
void UpdateCurrentItemIndex()
{
if (collectedItems.Count > 0)
{
currentItemIndex = Mathf.Clamp(currentItemIndex - 1, 0, collectedItems.Count - 1);
ActivateItemForAllClientsClientRpc(currentItemIndex);
}
else
{
currentItemIndex = -1;
HideAllItemsForAllClientsClientRpc();
}
}
[ClientRpc]
void NotifyItemDroppedForAllClientsClientRpc(string itemName, ulong networkObjectId, Vector3 position, Quaternion rotation)
{
if (NetworkManager.Singleton.SpawnManager.SpawnedObjects.TryGetValue(networkObjectId, out NetworkObject networkObject))
{
GameObject droppedItem = networkObject.gameObject;
if (droppedItem != null)
{
droppedItem.transform.position = position;
droppedItem.transform.rotation = rotation;
droppedItem.SetActive(true);
}
}
}
[ClientRpc]
void SyncItemsForAllClientsClientRpc()
{
Debug.Log("Syncing items...");
foreach (var item in allItems)
{
item.SetActive(false);
}
for (int i = 0; i < collectedItems.Count; i++)
{
SyncSingleItemForAllClientsClientRpc(collectedItems[i].name, true);
}
for (int i = 0; i < allItems.Count; i++)
{
if (!collectedItems.Contains(allItems[i]))
{
SyncSingleItemForAllClientsClientRpc(allItems[i].name, false);
}
}
}
[ClientRpc]
void SyncSingleItemForAllClientsClientRpc(string itemName, bool isActive)
{
GameObject itemInHand = allItems.Find(item => item.name == itemName);
if (itemInHand != null)
{
itemInHand.SetActive(isActive);
}
}
public void SwitchItem(int direction)
{
if (collectedItems.Count == 0) return;
currentItemIndex = (currentItemIndex + direction + collectedItems.Count) % collectedItems.Count;
HideAllItemsForAllClientsClientRpc();
ActivateItemForAllClientsClientRpc(currentItemIndex);
}
public bool HasItemInHand(string itemName)
{
return currentItemIndex >= 0 && currentItemIndex < collectedItems.Count && collectedItems[currentItemIndex].name == itemName;
}
[ServerRpc(RequireOwnership = false)]
public void RemoveItemFromHandServerRpc(string itemName)
{
if (HasItemInHand(itemName))
{
collectedItems[currentItemIndex].SetActive(false);
collectedItems.RemoveAt(currentItemIndex);
UpdateCurrentItemIndex();
}
}
public bool HasItemInInventory(string itemName)
{
return collectedItems.Exists(item => item.name == itemName);
}
}
Item.cs
using UnityEngine;
using Unity.Netcode;
public class Item : NetworkBehaviour
{
public string itemName;
public GameObject itemPrefab;
}
My prefab when I spawned it in the world:
list_of_prefabs
original_prefab_network_object
My prefab after I picked it up into the inventory and dropped it:
[Netcode] Failed to create object locally. [globalObjectIdHash=blablabla]. NetworkPrefab could not be found. Is the prefab registered with NetworkManager?
I expected that when the item is dropped, it would keep its original unique ID (or NetworkObjectId), allowing me to track it as the same object both in the inventory and when it is dropped in the game world.
2