nonexistent function 'get_children' in base 'Nil'

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

Hey i’m having an issue where this code:

onready var raycast_floor = $RaycastFloor

func _physics_process(delta):
    on_floor = check_on_floor()

func check_on_floor():
    for raycast_floor in raycast_floor.get_children():
        if raycast_floor.is_colliding():
            return true

    return false

is throwing an “Invalid call. Nonexistent function ‘get_children’ in base ‘Nil’”.

I believe Godot isnt detecting the child raycasts apart of the parent node RaycastFloor and I don’t know how to fix it. I could use is_on_floor() in place of the raycasts, but later on I plan on implementing wall jumping and since is_on_wall() doesnt work for what I have in mind, I’m stuck with this issue :confused:

Could anyone please help me with this?

:bust_in_silhouette: Reply From: Zylann
for raycast_floor in raycast_floor.get_children():

The error means raycast_floor is null. Usually it means $RaycastFloor is probably not the correct path due to a typo.

But also, looking closer… the fact you repeated twice the same variable name is very suspicious. I wonder if it should be warned by the script editor.

Try using a different name for the iteration variable, like this:

for child_raycast in raycast_floor.get_children():
    if child_raycast .is_colliding():
        return true

@Zylann - Looks like I’ve done it again. Started to answer an unanswered question, gotten sidetracked, and then came back a few minutes later to finish my answer - only then to find it’s already been taken care of.

Didn’t mean to provide a nearly duplicate answer a few minutes later… :frowning:

jgodfrey | 2020-03-06 14:46

yup changing the variable to child_raycast fixed it, I didnt see what I had done haha. I have another issue now, the Raycasts aren’t colliding with the TileMap despite sharing the same collision mask but i’ll give it a go and see what i come up with. Thanks for your help!

rodan | 2020-03-06 14:59

:bust_in_silhouette: Reply From: jgodfrey

Are you sure that the RaycastFloor node is an immediate child of the node containing the above script?

Also, this looks problematic:

for raycast_floor in raycast_floor.get_children():

You’re trying to store a child node reference in the same variable that contains your original RaycastFloor reference.

That should probably be something like:

for raycast_child in raycast_floor.get_children():

(And, then change any later references to the current raycast_child)

spot on with storing the value in the same variable, whoops. haha tyvm!

rodan | 2020-03-06 15:01