0 votes

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?

Godot version Godot 3.4.4 - Stable
in Engine by (12 points)

1 Answer

0 votes

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)
by (418 points)
edited by

It output the same error message as before.

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

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.