Godot can't find node

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

I’m trying to make a drag and drop “game” but when the main node collides with the other, it emits a signal but I keep getting an error saying that the node cannot be found. Here is all my code so far. I definitely made a mistake somewhere I just don’t know where.

extends Node2D

signal raycast_object_hit

var selected = false

var colUP = false
var colDOWN = false
var colLEFT = false
var colRIGHT = false

onready var rayUP = $UP
onready var rayDOWN = $DOWN
onready var rayLEFT = $LEFT
onready var rayRIGHT = $RIGHT

onready var attachment = get_node("RigidBody2D")

func _ready():
    attachment.connect("raycast_object_hit", self, _collision_detection())

func _on_Area2D_input_event(viewport, event, shape_idx):
    if Input.is_action_just_pressed("click"):
	    selected = true

func _physics_process(delta):

    _collision_detection()

    if selected:
	    global_position = lerp(global_position, get_global_mouse_position(), 25 * delta)

func _input(event):
    if event is InputEventMouseButton:
	    if event.button_index == BUTTON_LEFT and not event.pressed:
		    selected = false

func _collision_detection():

    var space_state = get_world_2d().direct_space_state
    var collision = space_state.intersect_ray(Vector2(0, 0), Vector2(50, 100))

    if rayUP.is_colliding():
        if colUP == false:
		    colUP = true
	    else:
		    colUP = false
	print("Collision")
	emit_signal("raycast_object_hit", rayUP.get_collider())

    if rayDOWN.is_colliding():
	    if colDOWN == false:
		    colDOWN = true
	    else:
		    colDOWN = false
	print("Collision")
	emit_signal("raycast_object_hit", rayDOWN.get_collider())

    if rayLEFT.is_colliding():
	    if colLEFT == false:
	    	    colLEFT = true
	    else:
		    colLEFT = false
	print("Collision")
	emit_signal("raycast_object_hit", rayLEFT.get_collider())

    if rayRIGHT.is_colliding():
	    if colRIGHT == false:
		    colRIGHT = true
	    else:
		    colRIGHT = false
	print("Collision")
	emit_signal("raycast_object_hit", rayRIGHT.get_collider())
:bust_in_silhouette: Reply From: r.bailey

The only place in this code you are calling for a node is

onready var attachment = get_node("RigidBody2D")

is this the proper node name in your project, get_node is relative to the current node, so if this node doesn’t have the “RigidBody2D” in it you would have to access it from wherever it is.

There is a link to someone wondering about something like this a while back,

Trouble understanding get_node