changing label text

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By takeda

I would like to create a score counter to my game through a label
here is my code:

extends Label

var score = 0

func _process(delta):
score += 1
get_node("HBoxContainer/score_number ").text = str(score)

but it wont work and gives me an error:
Invalid set index ‘text’ (on base: ‘null instance’) with value of type ‘String’.
thanks in advance!

:bust_in_silhouette: Reply From: jgodfrey

Your method of setting the text is correct (using the text property). However, the get_node() call is failing. So, the path you’ve provided to your node isn’t correct. If you can’t find the problem, post the layout of your scene tree and specify what node has the above script attached to it.

i couldn’t figure out how to upload an image, but the label is a child of an hbox container, which is a child of a spatial (the spatial is at the top) the script is attached to the label. i think the node path is correct becuase i right clicked the label and clicked copy path

takeda | 2020-12-15 21:46

If the script is attached to the label, then you’d just want:

text = str(score)

Since the script extends Label (I should have noticed that before), that’ll directly reference the text property on the Label.

jgodfrey | 2020-12-15 21:53

that seems to fix it, thanks!

takeda | 2020-12-15 22:53