I get these errors when I try to play my 2D game on unity the game works but I get these errors:
NullReferenceException: Object reference not set to an instance of an object Inventory..ctor () (at Assets/Scripts/Inventory.cs:13)
NullReferenceException: Object reference not set to an instance of an object Inventory..ctor () (at Assets/Scripts/Inventory.cs:13)
This are the script I'm using that contains the problem :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Inventory : MonoBehaviour
{
private List<Item> itemList;
public Inventory()
{
CustonGameEvent.GameEvents.gameEvents.onItemPurchase += AddItem;
itemList = new List<Item>();
Debug.Log("Inventory");
}
public void AddItem(Item item)
{
itemList.Add(item);
}
}
And this is the script that I'm using that has the Null line:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace CustonGameEvent
{
public class GameEvents : MonoBehaviour
{
public static GameEvents gameEvents;
private void Awake()
{
gameEvents = this;
}
public event Action<Item> onItemPurchase;
public void ItemPurchaseMade(Item ItemID)
{
if (onItemPurchase != null)
{
onItemPurchase(ItemID);
}
}
}
}