老师我在黑暗之光案例中用按键模拟拾取物品添加到背包中时,不能正常添加物品,并且报空指针的错误
NullReferenceException: Object reference not set to an instance of an object
InventoryItemGrid.SetId (Int32 id, Int32 num) (at Assets/Scripts/Custom/InventoryItemGrid.cs:19)
Inventory.GetId (Int32 id) (at Assets/Scripts/Custom/Inventory.cs:65)
Inventory.Update () (at Assets/Scripts/Custom/Inventory.cs:24)
代码如下:
public int id = 0;
private int num = 0;
private ObjectsInfo.ObjectInfo info = null;
private UILabel numLabel;
void Start() {
numLabel = this.GetComponentInChildren<UILabel>();
}
public void SetId(int id ,int num = 1) {
this.id = id;
info = ObjectsInfo._instance.GetObjectInfoById(id);
InventoryItem item = this.GetComponentInChildren<InventoryItem>();
item.SetIconName(info.icon_name);//这一条是报错跳转的代码
numLabel.enabled = true;
this.num = num;
numLabel.text = num.ToString();
}
//清空格子内的物品信息
public void CleraInfo() {
id = 0;
num = 0;
info = null;
numLabel.enabled = false;
}
public void PlusNumber(int num = 1) {
this.num += num;
numLabel.text = this.num.ToString();
}