KeyNotFoundException

Updated on May 5, 2017 in [A] Tutorials
Share on Facebook0Tweet about this on Twitter0Share on Google+0Share on Reddit0
0 on May 5, 2017

Everything was going perfect until I started receiving this error whenever I try to shoot player1 using player2 fps(built). It started at the end of e13(muzzle flash) when I created the hit particle effect and built and run the game.

“KeyNotFoundException: The given key was not present in the dictionary.
System.Collections.Generic.Dictionary`2[System.String,Player].get_Item (System.String key) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150)
GameManager.GetPlayer (System.String _playerID) (at Assets/scripts/multiplayer fps/GameManager.cs:42)
PlayerShoot.CmdPlayerShot (System.String _playerID, Int32 _damage) (at Assets/scripts/multiplayer fps/PlayerShoot.cs:121)
PlayerShoot.InvokeCmdCmdPlayerShot (UnityEngine.Networking.NetworkBehaviour obj, UnityEngine.Networking.NetworkReader reader)
UnityEngine.Networking.NetworkIdentity.HandleCommand (Int32 cmdHash, UnityEngine.Networking.NetworkReader reader) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkIdentity.cs:616)
UnityEngine.Networking.NetworkServer.OnCommandMessage (UnityEngine.Networking.NetworkMessage netMsg) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:1298)”

Here is my PlayerShoot’s source code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
[RequireComponent(typeof(WeaponManager))]
public class PlayerShoot : NetworkBehaviour {

private const string PLAYER_TAG = “Player”;

 [SerializeField]
private Camera cam;

[SerializeField]
private LayerMask mask;   // this allows in unity to control what we hit

private PlayerWeapon currentWeapon;
private WeaponManager weaponManager;

void Start()
{
if(cam == null)
{
Debug.LogError(“PlayerShoot: No Camera referenced!”);
this.enabled = false;   // disable the component
}

weaponManager = GetComponent<WeaponManager>();

}

void Update()
{
currentWeapon = weaponManager.GetCurrentWeapon();

if(currentWeapon.fireRate <= 0)
{
if(Input.GetButtonDown(“Fire1”))
{
Shoot();
}
}
else{
if(Input.GetButtonDown(“Fire1”))
{
InvokeRepeating(“Shoot”, 0f, 1f/currentWeapon.fireRate);
}
else if(Input.GetButtonUp(“Fire1”))
{
CancelInvoke(“Shoot”);
}
}

}

//Is called on the server when a player shoots
[Command]  //command is a function that is executed only on the server.
void CmdOnShoot()
{
RpcDoShootEffect();
}

//Is called on all clients when we need to do
//a shoot effect
[ClientRpc]  //all clients
void RpcDoShootEffect()
{
weaponManager.GetCurrentGraphics().muzzleFlash.Play();
}

//Is called on the server when we hit something
//Takes in the hit point and the normal of the surface
[Command]
void CmdOnHit(Vector3 _pos, Vector3 _normal)
{
RpcDoHitEffect(_pos, _normal);
}

//Is called on all clients
//Here we can spawn in cool effects
[ClientRpc]
void RpcDoHitEffect(Vector3 _pos, Vector3 _normal)
{
GameObject _hitEffect = (GameObject)Instantiate(weaponManager.GetCurrentGraphics().hitEffectPrefab, _pos, Quaternion.LookRotation(_normal));
Destroy(_hitEffect, 2f);
}

[Client]         //local method.never called on the server
void Shoot()
{

if(!isLocalPlayer)
{
return;
}

//We are shooting, call the onShoot method on the server
CmdOnShoot();

Debug.Log(“SHOOT!”);
RaycastHit _hit;
if(Physics.Raycast(cam.transform.position,cam.transform.forward, out _hit, currentWeapon.range, mask))  //if we shot something
{
if(_hit.collider.tag == PLAYER_TAG)
{CmdPlayerShot(_hit.collider.name,currentWeapon.damage);}

//We hit something, call the onHit method on the server
CmdOnHit(_hit.point, _hit.normal);
}
}

[Command]           //methods that are called only on the server
void CmdPlayerShot(string _playerID, int _damage)
{
Debug.Log(_playerID + ” has been shot.”);

Player _player = GameManager.GetPlayer(_playerID);
_player.RpcTakeDamage(_damage);
}

}

  • Liked by
Reply