Am I misunderstanding how @onready works?

Godot Version

Godot 4.3

Question

Ask your question here! Try to give as many details as possible

As I understand it from the docs @onready is called at the beginning of the _ready(), so that you can use it to reference another scenes.

I’m having a problem where the scenes that are refereced by @onready returns null, but if I rewrite it to be @export and set up the reference myself it works. I don’t understand why.

I thought the whole point of @onready was a way to avoid to set them up manually. I’m I missing something?

btw this is not a singelton/autoload, I saw in other posts that that could cause problems.

Below some of the code and the scene structure. The two first lines works after I changed them. Let me know if there is other context or info that can help.

@export var _titel: Label
@export var _val_fuel: Label

@onready var _val_hull: Label = $labels/val_hull
@onready var _val_crystals: Label = $labels/val_crystals
@onready var _val_barn: Label = $labels/val_barn
@onready var _val_dist: Label = $labels/val_dist


func _ready() -> void:
	update_labels()

func update_labels() -> void:
	_titel.text = "test name"
	_val_fuel.text = str(fuel)
	_val_hull.text = str(hull)
	_val_crystals.text = str(crystals)
	_val_barn.text = str(barnacles)
	_val_dist.text = str(distance)

are they being instantiated in the expected scene path?

Post the exact error messages.

Print get_path() in _ready() to check to which node the script is actually attached to. You might have accidentally attached it to some other node.

  1. Do the nodes actually exist at startup, or are they instantiated later such as in _ready()? @onready won’t work if the nodes don’t exist when it initiates.

  2. Try using get_node(). For example:
    @onready var _val_hull: Label = get_node(“labels/val_hull”)