Home > Software engineering >  Getting Error: NullReferenceException : Object reference not set to an instance of an object After p
Getting Error: NullReferenceException : Object reference not set to an instance of an object After p

Time:08-17

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

CodePudding user response:

This might be a partial solution, but you should try initializing itemList before assigning AddItem as the delegate to the onItemPurchase event. This is because the AddItem function tries to add an item to the itemList, which may not be initialized yet.

    public Inventory()
    {
        itemList = new List<Item>();

        CustonGameEvent.GameEvents.gameEvents.onItemPurchase  = AddItem;

        Debug.Log("Inventory");
    }

Also note what Hamid Yusifli said, using a constructor on a Monobehaviour will have unintended consequences, such as the contructor being called multiple times instead of once. For example, if you run this code:

using UnityEngine;

public class ConstructorTest : MonoBehaviour {

    public ConstructorTest() {
        Debug.Log ("Constructor is called");
    }

    void Awake () {
        Debug.Log ("Awake is called");
    }

    void Start () {
        Debug.Log ("Start is called");
    }

    void Update () {        
    }
}

It will print out this:

Constructor is called
Constructor is called
Awake is called
Start is called

The constructor is called twice which may mess up your code depending on what it contains.

CodePudding user response:

Don't use constructors to initialize MonoBehaviour.
MonoBehaviours should be initialized with special methods called Awake or Start, otherwise you may see unexpected results.

  • Related