Problems with Unity Networking Item Spawn

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật