Hello!
I am developing a game encountering Inventory, when I tried to drop things out, this happens:
KeyNotFoundException: The given key 'Inventory' was not present in the dictionary.
System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) (at <c0b7b90d34a54066a7234dad69255116>:0)
Unity.Netcode.NetworkBehaviour.__endSendServerRpc (Unity.Netcode.FastBufferWriter& bufferWriter, System.UInt32 rpcMethodId, Unity.Netcode.ServerRpcParams serverRpcParams, Unity.Netcode.RpcDelivery rpcDelivery) (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.9.1/Runtime/Core/NetworkBehaviour.cs:127)
Inventory.DropServerRpc (System.Int32 index, UnityEngine.Vector3 pos) (at Assets/Scripts/Inventory/Inventory.cs:90)
Inventory.Drop () (at Assets/Scripts/Inventory/Inventory.cs:77)
Inventory.Update () (at Assets/Scripts/Inventory/Inventory.cs:70)
Here’s the code:
public void Drop()
{
if (IsServer) DropServer(selectedSlot, PlayerController.Instance.transform.position);
else DropServerRpc(selectedSlot, PlayerController.Instance.transform.position);
}
public void DropServer(int index, Vector3 pos)
{
var r = slots[index].GetComponentInChildren<InventoryItem>().item.model.GetComponent<NetworkObject>().InstantiateAndSpawn(
NetworkManager, position: pos);
DropClientRpc(r);
}
[ServerRpc(RequireOwnership = false)]
public void DropServerRpc(int index, Vector3 pos)
{
DropServer(index, pos);
}
How can I fix it?