0 votes

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!

Godot version 3
in Engine by (30 points)

1 Answer

0 votes

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.

by (16,806 points)

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

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.

that seems to fix it, thanks!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.