Keep getting this error. NullReferenceException: Object reference not set to an instance of an object
Inventory.AddItem (UnityEngine.GameObject item) (at Assets/Scripts/Character/Inventory.cs:53)
Inventory.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/Character/Inventory.cs:45)
Here is my code:
public GameObject inventory;
public GameObject slotHolder;
private bool inventoryEnabled;
private int slots;
private Transform[] slot;
private GameObject itemPickedUp;
public void Start()
{
//Slots being detected
slots = slotHolder.transform.childCount;
slot = new Transform[slots];
DetectInventorySlots();
}
public void Update()
{
if (Input.GetKeyDown(KeyCode.Tab))
{
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 AddItem(GameObject item)
{
for(int i = 0; i < slots; i++)
{
if(slot*.GetComponent<Slots>().empty)*
{
slot*.GetComponent().item = itemPickedUp;*
slot*.GetComponent().itemIcon = itemPickedUp.GetComponent().icon;*
}
}
}
public void DetectInventorySlots()
{
for (int i = 0; i < slots; i++)
{
slot = slotHolder.transform.GetChild(i);
}
}