I can't convert int to string to use it with Label node.

Godot 4.2.1

I tried to use str() to convert a integer variable into a string and it didn’t work

I want to display data from an integer variable in a Label node, I tried using str() to convert it into a string, but it doesn’t seem to work. When I run the game it shows the following error: "Invalid set index ‘text’ (on base: “null instance”) with value of type ‘String’ ".

I don’t know if this is a bug with my version of Godot or something like that, if anyone can help me with this, I would be really glad.

I’m new to Godot and English is not my main language, so excuse me for any mistakes.

Here’s my example:

var integer_variable = 100

func _ready():
	$Label.text = str(integer_variable)
1 Like

I’m very new to Godot so please forgive me if I’m wrong, but I think it’s saying that $Label is null. It can’t set the value of .text because “null instance” does not have a .text property.

$name is shorthand for get_node(“name”). Is there definitely a Node (which is a child of to the one with this script) called Label?

2 Likes

Yes, it definitely was a Node with the same name I wrote when I found the error. The thing is that I was trying to do this from a autoload script, I am going to try from a script in a node from the same scene, thanks in any case :smiley:

1 Like

It worked!! Thaaaanks!! :face_holding_back_tears:

2 Likes

You’re welcome! Glad it worked