c# - 统一的Inventorysystem得到Nullreference错误
问题描述
我对编码很陌生,并且制作有趣的游戏,到目前为止一切顺利。
我现在正在研究库存系统,想从库存槽中做一个数组。我现在的问题是我得到一个 NullReferenceException: Object reference not set to an instance of an object error,就在 for 循环中。可悲的是,我没有看到或理解我的错误。我确实在 Unity 中设置了 det Inventoryslots,目前有 3 个。
*对不起,如果这是一个愚蠢的问题,我只是想学习
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InventorySlot : MonoBehaviour
{
public bool isFull;
public InventorySlot[] inventoryslots;
private Image Icon;
private bool foundSlot;
private Items items;
private ItemProperties itemProperties;
public int itemID;
public GameObject inventoryslot;
Items item = new Items();
public void inventorySlot()
{
}
public void Additem()
{
item.itemInList = itemID;
foundSlot = false;
for (int i = 2; i < inventoryslots.Length; i++)
{
if (!inventoryslots[i].isFull)
{
Icon.sprite = item.itemSprite;
inventoryslot.SetActive(true);
foundSlot = true;
}
}
if (foundSlot == false)
{
Debug.Log("No space");
}
}
}
解决方案
你初始化inventoryslots
变量了吗?你需要这样做,就像你对item
变量 in所做的那样Items item = new Items();
。
推荐阅读
- r - 在 Rstudio 中使用 keras 获得可重现的 LSTM
- npm - npm 退避库会在什么情况下尝试重试
- javascript - 如何将现有的 html 页面集成到 react-app 中?
- pattern-matching - 与地图内的地图进行模式匹配(Elixir)
- javascript - Sobel过滤器Javascript如何将黑色像素转换为白色和白色转换为黑色
- reactjs - 刷新页面时如何使 React 组件不更新?
- python - 如何部署 Azure 功能,并确保包得到更新
- java - 如何获取应用程序发送到网站的 xml 文件,并在测试中解析 xml 文件中的数据?
- json - 播音标签 API 调用
- c++ - 检查字符串中字符的所有实例?