Unity - A计划(一年有效期) 扫二维码继续学习 二维码时效为半小时

(66评价)
价格: 1299.00元
1对1 答疑即时回复7天无理由退款30天价保提问必答

服务保障

1对1 答疑

提供专属的一对一在线答疑,快速答疑解惑。

即时回复

及时响应学习咨询,帮助解决问题。

7天无理由退款

购买后7天内可申请无理由退款。

30天价保

购买后30天内如遇降价,可申请价保。

提问必答

对于提问做到有问必答,帮您扫清学习过程中的种种障碍

黑暗之光中,按键模拟拾取物品时空指针报错
塔里木河丶发起了问答2019-07-07
3
回复
676
浏览

老师我在黑暗之光案例中用按键模拟拾取物品添加到背包中时,不能正常添加物品,并且报空指针的错误

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();
    }

 

所有回复
  • 老师_Trigger • 2019-07-07

    同学你好,输出一下看看具体是什么报错,是Item,还是info还是info.icon_name,然后再做进一步的排查。

    • 塔里木河丶 • 2019-07-09

      老师我Print了一下info,然后报了null,但是好像没有错误,跟老师的代码是一样的

      (0) 回复
    • 老师_Trigger • 2019-07-09

      回复 @ 塔里木河丶: info空的话说明是GetObjectInfoById(id)这个方法没有获取到,检查一下这个方法里怎么解析数据的。实在不行直接赋值粘贴老师源码去进行测试。完成后再换回我们代码仔细检查。

      (0) 回复
    还有-3条回复,点击查看
    你还没有登录,请先登录或注册!
发表回复
你还没有登录,请先 登录或 注册!