Enabling a RayCast2D in GDscript

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

Hi, first time posting. I have this problem: My player has a RayCast2d child node and i want to enable it only when my player picks up a weapon from the ground. So the rifle scene is already connected and calls the player function when enters its area. This is the player’s fuction it calls:

func enable_weapon():
print("weapon enabled")
$RayCast2D.set_enabled(true)

The docs state that this is the method for enabling the raycast but i keep getting:

Attempt to call function 'set_enabled' in base 'null instance' on a null instance.

I have searched for solutions but i couldn’t fix it and i’ve been stuck with this for days now. Thanks in advance.

Is the node RayCast2D accessible from the script? Maybe you accessed the node incorrectly?

Ertain | 2020-09-30 00:35

when I get that error it is because I used either the wrong name or wrong address. did you change the name of the RayCast2d to something else or is the RayCast2D a child of a child in the scene tree?

ArthurER | 2020-09-30 01:03

:bust_in_silhouette: Reply From: OldKalash7

This is the node tree of the scene

KinematicBody2D
     Sprite
     CollisionShape2D
     Camera2D
     Raycast2D

The script it’s attached to the KinematicBody2D so i think that $RayCast2D would have to be enough to reference the node inside the script. I have checked and still i have the same issue.

I’ve just managed to solve it. Since looks like you are accessing a variable, not a function this is the correct syntax.

$RayCast2D.enabled = true

Anyway, thanks for your time people.

OldKalash7 | 2020-10-02 08:36