Invalid set index 'text' despite referencing correctly

Godot Version

4.2.2.stable

Question

I have already tried all of the suggested posts in this unsolved post here, but the issue I am having is that Godot is throwing a ‘null instance’ error related to two labels I have in my autoload UI. My UI code is:

extends CanvasLayer
class_name UI

@onready var health_label = $Health
@onready var experience_label = $Experience
@onready var death_ui = $DeathUI
@onready var win_ui = $WinUI

var player_hp: int:
	get : return Database.player_health
	set(value) : Database.player_health = value
var player_exp: int:
	get : return Database.player_exp
	set(value) : Database.player_exp = value
var scene = preload("res://scenes/main.tscn")

func _process(delta: float) -> void:
	health_label.text = "HP: " + str(player_hp)
	experience_label.text = "EXP: " + str(player_exp)
	do_visible_ui()

# Decides which player UI shows
func do_visible_ui() -> void:
	if GlobalVariables.death == true:
		death_ui.visible = true
		health_label.visible = false
		experience_label.visible = false
		GlobalVariables.can_control = false
	if GlobalVariables.flamagor_dead == true:
		win_ui.visible = true
		health_label.visible = false
		experience_label.visible = false
		GlobalVariables.can_control = false


# Restarts game if player wants
func _on_try_again_pressed() -> void:
	GlobalVariables.reset_variables()
	do_visible_ui()
	get_tree().reload_current_scene()


# Returns to main menu if not
func _on_give_up_pressed() -> void:
	get_tree().quit()

the specific error is stating that all of my labels and UIs (%Health, %Experience, %DeathUI, %WinUI) have invalid path names, despite being named as unique and accessible as such, and visible in the 2D editor. EDIT: they are currently set as $, but % does not work either.

This only started happening after putting my UI script in as an autoload, which I am newer to exploring, so I’m unsure if there’s some disconnect with the way autoloads handle nodes or not. Any help is appreciated!

Can you show your autoload panel? You should set the entire scene to autoload, not just the script.

2 Likes

As simple as it was, that was it! I had the script set in the autoload instead of the scene. Thank you!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.