1

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

}

Yumaisch
  • 17
  • 4
  • 1
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Broots Waymb Aug 11 '21 at 20:50

1 Answers1

0

Did you initialize the inventoryslots variable? You need to this, like you did with the item variable in Items item = new Items();.