Error at trying to get overlapping areas

Godot Version

Godot 4.2.2

Question

Hello! I was making a tile-based building system and I was trying to make an area detection so that you couldn’t place building if there already was one, but i get this error “Attempt to call function “get_overlapping_areas” in base “null instance” on a null instance.”
The code I tried was:

	if $Area2d.get_overlapping_areas().size() > 0:
		$Deny.show()
		$Ok.hide()
	else:
		$Ok.show()
		$Deny.hide()

And here is the full code I am using for building:

func _process(delta):
	var mouse_tile = Global.tilemap.local_to_map(get_global_mouse_position())

	var local_pos = Global.tilemap.map_to_local(mouse_tile)
	var world_pos = Global.tilemap.to_global(local_pos)
	global_position = world_pos
	if $Area.get_overlapping_areas().size() > 0:
		$Deny.show()
		$Ok.hide()
	else:
		$Ok.show()
		$Deny.hide()
	
func _unhandled_input(event):
	if event is InputEventMouseButton and event.is_pressed() and event.button_index == MOUSE_BUTTON_LEFT:

		set_process(false)
		set_process_unhandled_input(false)
		
		$Ok.hide()
		$Deny.hide()

Attempt to call function “get_overlapping_areas” in base “null instance” on a null instance.”

The error basically means that it could not find the Node “Area2d”, and thus can’t call get_overlapping_areas()

$Area2d is the same as calling get_node("Area2d")

Make sure that this Area2d node exists as a child of the node your script is attached to. This could also be a typo and the node is actually “Area2D” (the default name for the node).

1 Like