I cannot figure out why something that exists is null

Godot Version

4.4

Question

For the life of me, I cannot figure out why, during run-time, it’s complaining that “despawn_timer” is null.

@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
@onready var timer: Timer = $Timer
@onready var despawn_timer: Timer = %Despawn

signal walking
signal finished_walking

func _ready():
	if npc_data:
		apply_data()
		
	current_state = STATE.IDLE
	sprite.play("idle")
	timer.start() #THIS IS FINE

	#BUT THIS--WTF
	despawn_timer.set_wait_time(randi_range(30, 120))
	despawn_timer.start()

Please send help. I’m losing my mind.

Context: I’m preloading the scene and instantiating them via script.

e.g.

var npc_scene = preload("res://npc.tscn")
var npc = npc_scene.instantiate()
1 Like

I haven’t written anything in GDScript yet, but the only difference I can see in what you’ve shown is how you refer to the timers - one reference starts with $, the other with %. I don’t know how they work in GDScript though and what’s the difference.

The Sprite, which I gather also works, because you’re not complaining about it, also uses $.

If it’s not that, then it has to be a setting in the property inspector. Compare those between the two timers.

And kudos for making a minimum reproducible example! :slightly_smiling_face:

Oh that, I was trying different referencing methods but both gives me null.

FYI, $ gets node by path name, % gets node by its unique name which you must toggle via editor.

First thing I would do is try it without the scene unique identifier.
If this script is the NPC script it should work (as unique) UNLESS the NPC or the unique node is not part of the scene.
Remember that scene unique is only valid in the current scene.

A scene unique node can only be retrieved by a node inside the same scene.

Otherwise make sure the line you are pointing out is the one that is error-ing.

I have never felt the need for scene unique node naming. I always find other methods of exposing child nodes. (But this is neither here nor there)

No, I’m an idiot.

I had other scene with no “Despawn” timer nodes that inherited from this script. It was complaining about those ones.

Thanks everyone. Closing.