Moving items by the player does not work and produces the error.
Perhaps this is some kind of bug; my other projects had the same error. Hope someone can help me.
I use unity netcode for gameobjects.
Error:
KeyNotFoundException: The given key '0' was not present in the dictionary.
System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) (at <27124aa0e30a41659b903b822b959bc7>:0)
Dragger.SetJointConnectedClientRpc () (at Assets/Scripts/Items/Drag/Dragger.cs:58)
Here’s the code of Dragger.cs:
public Transform dragPosition;
public Transform _camera;
public float dragRange;
public NetworkVariable<NetworkObjectReference> currentDragObject = new NetworkVariable<NetworkObjectReference>(default, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Owner);
private CustomInput _input;
private void Start()
{
if (!IsOwner) return;
_input = GetComponent<CustomInput>();
}
private void Update()
{
if (!IsSpawned || !IsOwner) return;
if(Input.GetKeyDown(_input.dragItemKey))
{
if(currentDragObject.Value.NetworkObjectId != 0)
{
SetJointServerRpc();
currentDragObject.Value = new NetworkObjectReference();
return;
}
if(Physics.Raycast(_camera.position, _camera.forward, out RaycastHit hit, dragRange))
{
if(hit.collider.TryGetComponent(out NetworkObject networkObject))
{
if(hit.collider.TryGetComponent(out Rigidbody rigidbody))
{
if(hit.collider.TryGetComponent(out ConfigurableJoint joint))
{
currentDragObject.Value = networkObject;
SetJointConnectedServerRpc();
}
}
}
}
}
}
[ServerRpc(RequireOwnership = false)]
private void SetJointConnectedServerRpc()
{
SetJointConnectedClientRpc();
}
[ServerRpc(RequireOwnership = false)]
private void SetJointServerRpc()
{
ConfigurableJoint joint = NetworkManager.Singleton.SpawnManager.SpawnedObjects[currentDragObject.Value.NetworkObjectId].GetComponent<ConfigurableJoint>();
joint.connectedBody = null;
joint.xMotion = ConfigurableJointMotion.Free;
joint.yMotion = ConfigurableJointMotion.Free;
joint.zMotion = ConfigurableJointMotion.Free;
joint.angularXMotion = ConfigurableJointMotion.Free;
joint.angularYMotion = ConfigurableJointMotion.Free;
joint.angularZMotion = ConfigurableJointMotion.Free;
//SetJointClientRpc();
}
I don’t know how much more I need to write so that I can post my question
0