I am trying to make an inventory and line 30 is throwing an error when I play the game. Please help me understand how I can get it to work again because it was working until I made the ‘itemSlotContainer’ a prefab. Now that I remade it regular there’s still an issue. I also made sure to add the object in my game manager. Here is the error:
`NullReferenceException
UnityEngine.Object.Instantiate (UnityEngine.Object original, UnityEngine.Transform parent, Boolean instantiateInWorldSpace) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:190)
UnityEngine.Object.Instantiate[Transform] (UnityEngine.Transform original, UnityEngine.Transform parent, Boolean worldPositionStays) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:221)
UnityEngine.Object.Instantiate[Transform] (UnityEngine.Transform original, UnityEngine.Transform parent) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:216)
UI_Inventory.RefreshInventoryItems () (at Assets/Scripts/UI_Inventory.cs:30)
UI_Inventory.SetInventory (.Inventory inventory) (at Assets/Scripts/UI_Inventory.cs:20)
GameManager.Awake () (at Assets/Scripts/GameManager.cs:31)
`
using UnityEngine;
using UnityEngine.UI;
public class UI_Inventory : MonoBehaviour {
private Inventory inventory;
private Transform itemSlotContainer;
private Transform itemSlotTemplate;
private void Awake()
{
itemSlotContainer = transform.Find("itemSlotContainer");
itemSlotTemplate = itemSlotContainer.Find("itemSlotTemplate");
}
public void SetInventory(Inventory inventory)
{
this.inventory = inventory;
RefreshInventoryItems();
}
private void RefreshInventoryItems()
{
int x = 0;
int y = 0;
float itemSlotCellSize = 70f;
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 > 5)
{
x = 0;
y++;
}
}
}
}