Need help with inventory returning null

Godot Version

4.6.1

Question

I have been following this tutorial on inventory. https://www.youtube.com/watch?v=9CEUurKqfLQ&list=PLMQtM2GgbPEW__dODFVRPNF2TBry25IK4&index=5

All was working fine until I tried to add the collect function.

When I tried to find the problem, I am now seeing that my inventory is null.

I don’t understand because I am following the tutorial very closely, except for the hurt box (which I replaced)

Inventory script is a resource as seen below.

Does anyone know why the inventory is showing up as null? I placed the inventory array as an export on the player.

If you share code, please wrap it inside three backticks or replace the code in the next block:

@export var itemRes: InventoryItem
@onready var inventory: Inventory

func _on_area_2d_area_entered(other_area_2d: Area2D) -> void:
	print("area entered")
	collect(inventory)
	queue_free()

func collect(inventory: Inventory):
	inventory.insert(itemRes)
	if inventory == null:
		print("inventory is null!")
		return


extends Resource
#maker tech #2
class_name Inventory

@export var items: Array[InventoryItem]

func insert(item: InventoryItem) -> void:
	pass

Your .insert() function in the inventory is empty.

Follow the rest of the tutorial.

Does’nt work. It is the inventory part that is not working. It is showing null in members and local in the stack trace.

func insert(item: InventoryItem) → void:
pass

Where it says pass: write

Items.append(InventoryItem)

Where do you initialize the variable inventory with an actual reference to an Inventory object?

Invalid call. Nonexistent function ‘insert’ in base ‘Nil’.

not sure if you mean this?

sorry misspoke

my items in the inventory array are resources

In the code you posted inventory is never initialized. That’s why it is null.

hmmmm, i followed the tutorial exactly and the only thing i changed was not including the func on_hurt_box_area_entered(area): if area.has_method(“collect”):
area.collect()

Well then, put that function back in and see if it works. If it doesn’t, it means that you didn’t follow it exactly or that the tutorial itself is broken.

i put it in. it is still getting stuck at the inventory function

You’ll have to revise the tutorial steps and check if you did everything as instructed.

unfortunately already did that, thanks for trying

The tutorial calls collect() from the player’s script, while you are trying to call it from the collectible item itself.

Yeah, you’ll need to do it yet again, paying more attention, because you likely missed something.

I placed it all in the player script instead, and it cant recognize the items now. it says null in items.

What do you mean by “all”? Only the part that calls collect() should be in the player script, the collect() function itself needs to stay in the item script.

Look again how the tutorial is writing that function. You need to call collect() on the area, and you surely don’t want to queue_free() the player.