问题描述
所以我一直在我的游戏中创建库存, 我一直在关注教程,https://www.youtube.com/watch?v=2WnAOV7nHW0 我在 8:40 被卡住的地方,
我收到一个空错误,NullReferenceException:未将对象引用设置为对象的实例
Image image = itemSlotRectTransform.Find("image").GetComponent<Image>();
这是完整的代码,来自脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UI_Inventory : MonoBehavIoUr
{
private Inventory inventory;
private Transform itemSlotContainer;
private Transform itemSlottemplate;
public void Awake()
{
itemSlotContainer = transform.Find("itemSlotContainer");
itemSlottemplate = itemSlotContainer.Find("itemSlottemplate");
}
public void SetInventory (Inventory inventory)
{
this.inventory = inventory;
RefreshInventoryItems();
}
private void RefreshInventoryItems()
{
int x = -1;
int y = 1;
float itemSlotCellSize = 255f;
foreach (Item item in inventory.GetItemList())
{
RectTransform itemSlotRectTransform = Instantiate(itemSlottemplate,itemSlotContainer).GetComponent<RectTransform>();
itemSlotRectTransform.gameObject.SetActive(true);
itemSlotRectTransform.anchoredPosition = new Vector2(x * itemSlotCellSize,y * itemSlotCellSize);
**Image image = itemSlotRectTransform.Find("image").GetComponent<Image>();**
image.sprite = item.GetSprite();
x++;
if (x > 4)
{
x = 0;
y++;
}
}
}
}
This is the photo of my inspector where the sprites,are attached to
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)