Cannot all method add_child() on null value

notif.gd

@onready var text: PackedScene = preload("res://Scenes/label.tscn")
@onready var notif_container: VBoxContainer = $Notif_Panel/Notif_Scroll_Container/Notif_Container

func add_msg(message: String):
	var temp_text = text.instantiate()
	temp_text.text = message
	notif_container.add_child(temp_text)

I have been trying to get this to work I can get it to work if I directly put it into where I get the messages from but if I do that is is a lot of wasteful code
I know this should work but it just refuses
I have tried to duplicate a label, and make a label now I am using scenes and it still refuses
I am not using this code anywhere else except to get messages when things happen this is a global script so I just do notif.add_msg(“Hello”) for example

Global scripts @onready functions are run before anything in the tree is available. It is kinda the point of globals. So your notif_container wil be null.

When you call this global why not send a node reference to the target node (which the requesting node must surely know about) along with the message. This might not work for whatever your structures, usage or intentions are, but is one approach. Might also make your message system more flexible.

func add_msg(message: String, notif_container: Node):
1 Like

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