Home>
I get an error when I hit any object on the stage:
KeyNotFoundException: The given key was not present in the dictionary.
System.Collections.Generic.Dictionary`2 [TKey, TValue] .get_Item (TKey key) (at <
695d1cc93cca45069c528c15c9fdd749 >
: 0)
GameManager.GetPlayer (System.String _playerID) (at Assets /Scripts /GameManager.cs: 39)
PlayerShoot.CmdPlayerShot (System.String _playerID, System.Int32 _damage) (at Assets /Scripts /PlayerShoot.cs: 51)
PlayerShoot.CallCmdPlayerShot (System.String _playerID, System.Int32 _damage) (at <
15d409b66be84a30afe6c008fd1cb636 >
: 0)
PlayerShoot.Shoot () (at Assets /Scripts /PlayerShoot.cs: 41)
PlayerShoot.Update () (at Assets /Scripts /PlayerShoot.cs: 29)
Here is the code for the script itself:
using UnityEngine;
using System.Collections.Generic;
public class GameManager: MonoBehaviour
{
public static GameManager instance;
public MatchSettings matchSettings;
private void Awake ()
{
if (instance!= null)
{
Debug.LogError ("More than one GameManager in scene.");
} else
instance= this;
}
#Region Player tracking
private const string PLAYER_ID_PREFIX= "Player";
private static Dictionary <
string, Player >
players= new Dictionary <
string, Player >
();
public static void RegisterPlayer (string _netID, Player _player)
{
string _playerID= PLAYER_ID_PREFIX + _netID;
players.Add (_playerID, _player);
_player.transform.name= _playerID;
}
public static void UnRegisterPlayer (string _playerID)
{
players.Remove (_playerID);
}
public static Player GetPlayer (string _playerID)
{
return players [_playerID];
}
//void ingui ()
//{
//guilayout.beginarea (new rect (200, 200, 200, 500));
//guilayout.beginvertical ();
//foreach (string _playerid in players.keys)
//{
//guilayout.label (_playerid + "-" + players [_playerid] .transform.name);
//}
//guilayout.endvertical ();
//guilayout.endarea ();
//}
#Endregion
}
Yes, it is clear from the text of the error
Эникейщик2021-12-22 19:06:57@KuzCode sorry, I'm just getting started, I'm sitting at 16 trying to start understanding the code, c#, c++ Added GameManager script.
Дмитрий Громовой2021-12-22 19:11:19@ Enikeyschik I do not argue, but in order to indicate to the author about an error in the code, it is desirable that he showed the code, obviously the error is that the key is not in the list
KuzCode2021-12-22 20:10:17return players [_playerID] you pass here a key that is not in the dictionary, put a breakpoint on this line and see what value there is
KuzCode2021-12-22 20:11:26Related questions
- c# : Unity | Can't add getting coins for watching rewarded ads
- c# : Inheriting a component from a superclass
- c# : The Strategy pattern in Unity. How to fit the logic of click-to-move motion into this pattern?
- c# : Overlay background on top of each other
- c# : Incorrect Unity3D motion calculation
- c# : InvalidCastException: Specified cast is not valid
- c# : How do I get a component in Unity through a string variable?
- c# : Is it correct to write logic in scriptableObject?
- c# : How to walk both up and down?
error in GameManager.cs script, show it
KuzCode2021-12-22 19:06:40