在做背包出现错误了,自学的实在是不知道哪里错了求大神指点
错误是:
ArgumentException: An element with the same key already exists in the dictionary.
ArgumentException:一个元素键已经存在相同的字典。
这个是脚本
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class CharacterUP : MonoBehaviour
{
public static Dictionary<int, BaseItme> ItemList;
private GameObject item;
public GameObject[] UCells;
private Image imagesingle;
void Awake()
{
Load ();
}
// void Disable()
// {
//
// }
void Start ()
{
}//end_Start
void Update ()
{
if (Input.GetKeyDown(KeyCode.X)) {
int index = Random.Range (0, 7);
Pickup (ItemList[index]);
}
}//end_Update
private void Load()
{
ItemList = new Dictionary<int, BaseItme> ();
Weapons hudie = new Weapons (0,"蝴蝶","+30点攻击力\n+30%攻速\n+30点敏捷\n+35%的物理攻击闪避",6000,3000,"picture/hudie",30);
Weapons dadian = new Weapons (1,"雷神之锤","+24点攻击力\n+80%攻击速度\n",5600,2800,"picture/dadian",24);
Weapons xiwa = new Weapons (2,"西瓦","+15点护甲\n+30点智力",4700,2600,"picture/xiwa",15);
Weapons qiangxi = new Weapons (3,"强袭","+10点护甲\n+35%攻速",5250,3100,"picture/qiangxi",10);
Cuns chishu = new Cuns (4,"古树","回复HP",125,50,"picture/chishu",175, 0);
Cuns jinghu = new Cuns (5,"精华药水","回复MP",50,25,"picture/jinghu",0,135);
ItemList.Add (hudie.ID, hudie);
ItemList.Add (dadian.ID, dadian);
ItemList.Add (xiwa.ID, xiwa);
ItemList.Add (qiangxi.ID, qiangxi);
ItemList.Add (chishu.ID, chishu);
ItemList.Add (jinghu.ID, jinghu);
}
public void Pickup(BaseItme baseItme)
{
item = Instantiate (Resources.Load("Prefabs/Uitem"), transform.position, transform.rotation) as GameObject;
imagesingle = item.transform.GetComponent<Image> ();
imagesingle.overrideSprite = Resources.Load (baseItme.Icon, typeof(Sprite)) as Sprite;
item.transform.SetParent (UCells[0].transform);
item.transform.localPosition = Vector3.zero;
}
}
错误是:
ArgumentException: An element with the same key already exists in the dictionary.
ArgumentException:一个元素键已经存在相同的字典。
这个是脚本
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class CharacterUP : MonoBehaviour
{
public static Dictionary<int, BaseItme> ItemList;
private GameObject item;
public GameObject[] UCells;
private Image imagesingle;
void Awake()
{
Load ();
}
// void Disable()
// {
//
// }
void Start ()
{
}//end_Start
void Update ()
{
if (Input.GetKeyDown(KeyCode.X)) {
int index = Random.Range (0, 7);
Pickup (ItemList[index]);
}
}//end_Update
private void Load()
{
ItemList = new Dictionary<int, BaseItme> ();
Weapons hudie = new Weapons (0,"蝴蝶","+30点攻击力\n+30%攻速\n+30点敏捷\n+35%的物理攻击闪避",6000,3000,"picture/hudie",30);
Weapons dadian = new Weapons (1,"雷神之锤","+24点攻击力\n+80%攻击速度\n",5600,2800,"picture/dadian",24);
Weapons xiwa = new Weapons (2,"西瓦","+15点护甲\n+30点智力",4700,2600,"picture/xiwa",15);
Weapons qiangxi = new Weapons (3,"强袭","+10点护甲\n+35%攻速",5250,3100,"picture/qiangxi",10);
Cuns chishu = new Cuns (4,"古树","回复HP",125,50,"picture/chishu",175, 0);
Cuns jinghu = new Cuns (5,"精华药水","回复MP",50,25,"picture/jinghu",0,135);
ItemList.Add (hudie.ID, hudie);
ItemList.Add (dadian.ID, dadian);
ItemList.Add (xiwa.ID, xiwa);
ItemList.Add (qiangxi.ID, qiangxi);
ItemList.Add (chishu.ID, chishu);
ItemList.Add (jinghu.ID, jinghu);
}
public void Pickup(BaseItme baseItme)
{
item = Instantiate (Resources.Load("Prefabs/Uitem"), transform.position, transform.rotation) as GameObject;
imagesingle = item.transform.GetComponent<Image> ();
imagesingle.overrideSprite = Resources.Load (baseItme.Icon, typeof(Sprite)) as Sprite;
item.transform.SetParent (UCells[0].transform);
item.transform.localPosition = Vector3.zero;
}
}