根据我的理解,这里应该发生的事情会在评论中解释,但由于某些原因我在编辑器中出现了这个错误
KeyNotFoundException:字典中没有给定的键。 System.Collections.Generic.Dictionary`2 [System.Int32,System.String] .get_Item (Int32键)(at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150) UpgradeManager.ScanForAvailableUpgrades()(at Assets / Scripts / UpgradeManager.cs:125 UpgradeManager.Update()(at 资产/脚本/ UpgradeManager.cs:70)
在调用函数DisableSameType
后发生错误
public string GetUpgradeType(string upgradeName){
return upgradeTypes [upgradeName];
}
public void DisableSameType (string upgradeName){
string upgradeType = GetUpgradeType (upgradeName);
string[] keys = KeyByValue (upgradeTypes, upgradeType); //returns all the keys of the same type
if (keys.Length == 0) { //if there are no keys
return; //then return
} else {
for (int i = 0; i < keys.Length; i++) { //loop through all the keys
upgradesOwned [keys [i]] = false; //Set them to false
}
}
}
以下是其他相关代码
public List<string> AvailableUpgrades = new List<string>();
protected Dictionary<int,string> upgradeNames = new Dictionary<int, string>();
protected Dictionary<string,bool> upgradesOwned = new Dictionary<string, bool>();
protected Dictionary<string,bool> upgradesPurchased = new Dictionary<string, bool>();
protected Dictionary<string,int> upgradeCost = new Dictionary<string, int>();
/* UPGRADE TYPES
* Title
* Layout
* Scoreboard
* VisualCountdown
*/
protected Dictionary<string,string> upgradeTypes = new Dictionary<string, string>();
protected bool IsPurchased(string upgradeName){
return upgradesPurchased [upgradeName];
}
public static string[] KeyByValue(Dictionary<string, string> dict, string val)
{
string key = null;
foreach (KeyValuePair<string, string> pair in dict)
{
if (pair.Value == val)
{
key += pair.Key + "|";
break;
}
}
return key.Split('|');
}
protected void ScanForAvailableUpgrades(){
AvailableUpgrades.Clear (); //Clear the list
int scanRange = GetUnits() + GameManager.Instance.scanRange; //Get the range by adding the scanRange to current units
for (int i = 0; i < upgradesOwned.Count; i++) { //Loop through all upgrades
if (IsPurchased (upgradeNames [i])) {
//return
} else {
if (scanRange >= upgradeCost [upgradeNames [i]]) { //Check if the cost is within the scanrange
if (AvailableUpgrades.Contains (upgradeNames [i])) { //Check if upgrade is already in the available list
//return; //If it is we just return
} else { //Else
if (upgradesOwned [upgradeNames [i]]) { //Check if the user already has the upgrade *NOTE* Should probaby check for this first to improve performance
//return; //If it is return
} else { //Else
AvailableUpgrades.Add (upgradeNames [i]); //Upgrade does not exist and is not bought so add it to the available list
}
}
}
}
}
}
在进一步调试后,我发现Debug.Log(keys[i]);
将第一个字符串作为“basicTitle |”返回并且字符串字符串为空
编辑:
我可以看到我的问题似乎误导了第125行是这一行if (IsPurchased (upgradeNames [i])) {
但是我已经完成并且在评论出“DisableSameType”之后错误消失了方法DisableSameType
是一个新函数并且错误没有开始发生,直到它被实施,所以我100%肯定这是错误的来源,我只是不确定是什么导致它
答案 0 :(得分:1)
key
词典中没有有效的upgradesOwned
在将key value
设置为false
之前,您应该检查dictionary
是否有key
。如果它有key
,那么您可以false
,否则您需要在Add
中dictionary
键值对。
所以,代替代码中的这一行:
upgradesOwned [keys [i]] = false; //Set them to false
应该是这样的:
if(upgradesOwned.ContainsKey(keys[i])){
upgradesOwned [keys [i]] = false; //Set them to false
}else{
upgradesOwned.Add(keys [i],false); //Add new key and set them to false
}