same errors with different code

I copied some code I used from one game to another and some of it works correctly and the inventory/ui system i implemented keeps having the same error. This is the example I currently have with this error.

NullReferenceException: Object reference not set to an instance of an object
PlayerInteract.Awake () (at Assets/Scripts/PlayerInteract.cs:16)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerInteract : MonoBehaviour
{
    Collider2D item;
    private Inventory inventory;
    [SerializeField] private UI_Inventory uiInventory;


    private void Awake()
    {

        inventory = new Inventory();
        uiInventory.SetInventory(inventory);

        ItemWorld.SpawnItemWorld(new Vector3(2.5f, 3.5f), new Item { itemType = Item.ItemType.Fireball, amount = 1 });
        ItemWorld.SpawnItemWorld(new Vector3(2.0f, 3.5f), new Item { itemType = Item.ItemType.Potion, amount = 1 });

    }

    //private void Update()
    //{
    //    if (currentInterObj && Input.GetButtonDown("Interact"))
    //    {
    //        currentInterObj.SendMessage("DoInteraction");
    //    }
    //}

    private void OnTriggerEnter2D(Collider2D collider)
    {
        ItemWorld itemWorld = collider.GetComponent<ItemWorld>();
        if (itemWorld != null)
        {
            inventory.AddItem(itemWorld.GetItem());
            itemWorld.DestroySelf();
        }
    }
}

The Inventory code is:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;

public class Inventory
{
    public event EventHandler OnItemListChanged;

    private List<Item> itemList;


    public Inventory()
    {
        itemList = new List<Item>();
        //AddItem(new Item { itemType = Item.ItemType.Fireball, amount = 1 });
        //AddItem(new Item { itemType = Item.ItemType.Potion, amount = 1 });
        //AddItem(new Item { itemType = Item.ItemType.Fireball, amount = 1 });
        //AddItem(new Item { itemType = Item.ItemType.Potion, amount = 1 });
    }

    public void AddItem(Item item)
    {
        itemList.Add(item);
        OnItemListChanged?.Invoke(this, EventArgs.Empty);
    }

    public List<Item> GetItemList()
    {
        return itemList;
    }
}

The original video that I took the code from is a Code Monkey video.

I think you need to initialize your uiInventory
uiInventory = new uiInventory();

1 Like

Thanks but that didn’t do it :confused:

Since line 16 is your error line, then @Agent003 is correct. uiInventory is null. You made it a SerializeField though, so either it’s being overwritten in the inspector, which makes it null, or you forgot to drag something into that slot.

If uiInventory is a monobehaviour, then you can’t initialize it with new, you’d have to do some GetComponent or AddComponent type call, but I’m guessing since it’s a SerializeField, you just need to drag the gameObject in your scene that has the script into the slot.

Take a look at about 5:46 in the video to see what I’m talking about(after the youtuber creates the variable). Remember when following tutorials to follow it all the way or little details will be missed.

1 Like

So I see what you did there and yes I think I edited the code and it took the object out of the script. I put it back in and the error is gone. Thanks for that. Its giving me the same error for a different script. We could probably do this all day and there is no object that needs to be added to this script. BTW love the quick results and the support. Much appreciated. I am going to look through the video some more and see what I could possibly be doing wrong with all my scripts and make sure I didn’t miss anything.