How to access text property of a RichTextLabel from a seperate script?

Godot Version

v4.1.3.stable.official [f06b6836a]


How can I update the text property a RichTextLabel (MyRTL) from the in the below example?

I am trying to change the text property on a Rich Text Label from a script that’s separate to it, but I get the following error:

Invalid set index ‘text’ (on base: ‘null instance’) with value of type ‘String’.

I have tried a few things:

  1. Adding a node2d that does nothing but link to the script
  2. Using classes, ie. preloading the additional script in the script, making a new instance of the script, then accessing the function that updates the label from the main scene.
  3. Tried making an autoload script in the project settings

I’ve tried setting up a test project to figure it out with no luck too.

My scene tree looks like this:


When editing I can drag and drop from the scene tree into the script fine and the path looks correct, but it crashes at runtime with the error. I can even do .text and the editor sees it, but again at runtime it fails. How can I resolve this?


The project has just two scripts:

extends Node2D

const snode = preload("res://")

func _ready():
	var nodeinstance =

extends Node

func myfunc():
	# try to update RTL from here
	$"../HUD/Background/ControlsContainer/HBoxContainer/MyRTL".text = "tea time"

I’m not quite sure what’s going on with your scene, in part because the way you’re going about it isn’t how I’ve done it before. Have you tried using an exported variable to reference the RichTextLabel inside

@export var label: RichTextLabel

Then you can drag-and-drop that into that field in the inspector and reference it like a RichTextLabel, if all works as expected, i.e.

func myfunc():
    label.text = "tea time"

I’ve tried this but I get the error:

Parser Error: Identifier “label” not declared in the current scope.

I also tried dragging the MyRTL label to this field

I’m not sure that I’m going about it the right way, as I’m new to godot (but not programming) so some of my bad habits might be coming across.

instantiate() will not work in this case, as it works by unpacking the PackedScene and accessing it, not the node itself. That is, you can add your RTL node to the scene through it and use myfunc() in that case. However, if you want to use a ready-made variant, use @onready var rtl = NodePath to RTL
I think this should help

Not sure if you have multiple scripts or just changed the names for the post, but that Label is in, and the other code was in

Multiple scripts, is attached to the MainScene node, is attached to the “scriptnode” node. I’m trying to update the MyRTL’s text property from

If that’s the case, the two code snippets I posted above need to both be inside and the MyRTL that’s in your screenshot needs to be set in the node in the Inspector.

In main_script:
extends Node2D
@onready var ScriptNode = NodePath to script_node

func _ready() :

In script_node:
extends Node2D
@onready var rtl = NodePath to RTL
func myfunc():
rtl.text = “tea time”

Thanks for this but unforunately it gives me the error:

Attempt to call function ‘myfunc’ in base ‘null instance’ on a null instance.

extends Node2D

@onready var ScriptNode = $scriptnode

func _ready() :

extends Node2D

@onready var rtl = $"../HUD/Background/ControlsContainer/HBoxContainer/MyRTL"

func myfunc():
	rtl.text = "tea time"

If this is not possible, you can write custom signals and methods to handle them.

I ended up making a new project again from scratch and copying these screenshots exactly and it worked. Weirdly, going back to my first example and making the exact same changes didn’t work. However, going back to my actual game project and doing those changes also worked so I’m happy.

Thank you both for helping me with this problem, I’ve been tearing my hair out for a few hours trying to figure it out.

1 Like

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