I'm quite new to coding, and making a game for fun, it went well until now.
I'm working on the inventorysystem right now and wanted to do an array out of inventoryslots. The problem for me right now is that i get a NullReferenceException: Object reference not set to an instance of an object error, right att the for loop. Sadly I don't see or understand my mistake. I do set det Inventoryslots in Unity and have 3 at the moment.
*Sorry if this is a stupid question i just want to learn
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");
}
}
}