i cannot call the function label.hide() because the variable label is null

Godot Version

ver 4.6.1

Question

i tried to follow a tutorial called cute and simple dialog box but something does not work. I am having a lot of issues because the variable label on the dialog manager script results null, and because of that some functions do not work. here’s the script:

extends Node2D

@onready var player = get_tree().get_first_node_in_group("player")
@onready var label: Label = $Label




const base_text = "[E] to"

var active_areas = []
var can_interact = true

func register_area(area: InteractionArea):
	active_areas.push_back(area)
	
func unregister_area(area: InteractionArea):
	var index = active_areas.find(area)
	if index != -1:
		active_areas.remove_at(index) 
		
func _process(delta):
	if active_areas.size() > 0 && can_interact:
		active_areas.sort_custom(_sort_by_distance_to_player)
		label.text = base_text + active_areas[0].action_name
		label.global_position = active_areas[0].global_position
		label.global_position.y -= 36
		label.global_position.x -= label.size.x / 2
		label.show()
	else:
		label.hide()
		
func _sort_by_distance_to_player(area1, area2):
	var area1_to_player = player.global_position.distance_to(area1.global_position)
	var area2_to_player = player.global_position.distance_to(area2.global_position)
	return area1_to_player < area2_to_player
	
func _input(event):
	if event.is_action_pressed("interact") && can_interact:
		if active_areas.size() > 0:
			can_interact = false
			label.hide()
			await active_areas[0].interact.call()
			can_interact = true

72 Errors and warnings? One of them will tell you the reason for your problem. Try to fix them and post the relevant error to your label, please.

1 Like

If this is a Global/Autoload you may have added the script .gd instead of the scene .tscn. adding the script as a global only adds the script, no children nodes. Your script also extends Node2D but it looks like your scene tree the text box is a MarginContainer not a Node2D.

1 Like

A likely reason for the 72 errors too.