Setting text to a label causing error

Godot Version

4.7.2 stable

Question

Trying to update the text of a label and keep receiving this error how do i assign it to string rather than null

dirt_tag.text = str("Dirt: ", Items.dirt)

However, make sure dirt is not null, it seems to be.

Still getting the error after trying this, Here is my item script im pulling from

Where do you initialize Items? I can’t see it anywhere, and it doesn’t seem like Items is a variable in-scope.

The items are a global script i assumed they were initialised with that

I think the main problem is that you’re missing the following line from the top of the items.gd file:

class_name Items

It’s been a while since I used GDScript, but I believe you need to use that in order to be able to access said properties.

adding this breaks the global script “Class “Items” hides an autoload singleton.”

Could it be that you forgot to assign dirt_tag in the godot inspector? Since it’s an Export variable.

its assigned in the original image

the scene runs fine if im not trying to assign text to the label on ready

Could you put a print statement in the _ready function, and simply run the game? The only thing I can think of is that maybe you have this same main.gd script attached to another node that you don’t know about.

attempting to print in _ready causes a crash

That’s not normal. So simply putting print("something") in ready crashes your game?

Oh no i meant update the text

Attempting to run the update_text() function from another script also crashes the game

For now, simply remove update_text() from your ready function, and simply put this there:

func _ready() -> void:
	print("test")

Then run your game. Does “test” only get printed once, or multiple times?

it gets printed twice

That means you have another Node which has this same main.gd script attached to it. You need to either remove that, or if it’s intentional, that second node probably doesn’t have the label properly set in the inspector.

only my main node has the script attached

If “test” gets printed twice, then you have another node which has the script attached to it. While your game is running, go back to Godot and on the left side, click on “Remote”, that will show your full scene tree while your game is running, so you can check what nodes actually exist during runtime. Chances are you have two main.gd nodes somewhere.