how to fix Get_node() returning null even with correct path

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ThatOneGuyNamedJoe

I’m trying to get the children of a node and its returning the error message:
“Attempt to call function get_children in base ‘null instance’ on null instance”

The node path is correct and I copied the path directly from node so it doesn’t seem like there is a mistake with that.

Here is the code:

func _ready():
for inv_slot in $InventorySlots.get_children():
	inv_slot.connect("gui_input", self, "slot_gui_input", [inv_slot])

And the scene tree looks like this(I don’t know how to upload photos):

Inventory → InventorySlots → Slots 1-25

(The script is attached to the “Inventory” Node)

Is there anyway to fix this?

:bust_in_silhouette: Reply From: Pomelo

When I need to acces the all teh childs of a node I do this

for i in Node.get_child_count():
    var node = Node.get_child(i)
    # do your stuff

Not sure if get_children() does the same? maybe it does. If what I wrote doesnt work, try this please and let me know what is the output:

func _ready():
    for i in $InventorySlots.get_child_count():
        var node = $InventorySlots.get_child(i)

It output the same error message as before.

ThatOneGuyNamedJoe | 2022-07-17 01:05

but what are the print outputs? for example print($InventorySlots).

If you just do func _ready(): print($InventorySlots) and it shows the error, then the problem is that godot cant find that node (probably a typo?). Thats why I asked you to show me all the prints, to see where the problem is

Pomelo | 2022-07-18 13:43

When I ran the line it output:


Also it shouldn’t be a typo because I copied the root directly from the node unless the script is being executed in another scene(This scene is the only instance of than script so it shouldn’t be).

I tried print(self.get_children()) to make sure that the InventorySlots node is in fact a child and it output this so I cant seem to find the issue.


ThatOneGuyNamedJoe | 2022-07-20 08:46