I get this:
NullReferenceException: Object reference not set to an instance of an object Inventory.AddItem (UnityEngine.GameObject item) (at Assets/scripts/Inventory.cs:63) Inventory.OnTriggerEnter (UnityEngine.Collider other) (at Assets/scripts/Inventory.cs:47)
Whenever I try to pick up items with this script.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Inventory : MonoBehaviour
{
public GameObject inventory;
public GameObject slotHolder;
private bool inventoryEnabled;
public int slots;
private Transform[ ] slot;
private GameObject itemPickedUp;
private bool itemAdded;
public void Start()
{
//slots being detected
slots = slotHolder.transform.childCount;
slot = new Transform[slots];
DetectInventorySlots();
}
public void Update()
{
if (Input.GetKeyDown(KeyCode.I))
{
inventoryEnabled = !inventoryEnabled;
}
if (inventoryEnabled)
inventory.SetActive(true);
else
inventory.SetActive(false);
}
public void OnTriggerEnter(Collider other)
{
if (other.tag == “Item”)
{
print(“Colliding!”);
itemPickedUp = other.gameObject;
AddItem(itemPickedUp);
}
}
public void OnTriggerExit(Collider other)
{
if (other.tag == “Item”)
{
itemAdded = false;
}
}
public void AddItem(GameObject item)
{
for (int i = 0; i < slots; i++)
{
if (slot*.GetComponent().empty && itemAdded == false)*
{
slot*.GetComponent().item = itemPickedUp;*
slot*.GetComponent().itemIcon = itemPickedUp.GetComponent().icon;*
itemAdded = true;
}
}
}
public void DetectInventorySlots()
{
for (int i = 0; i < slots; i++)
{
slot = slotHolder.transform.GetChild(i);
}
}
}