Invalid assignment of property or key 'text' with value of type 'String' on a base object of type 'Label'.

Godot Version

` 4.3

Question

` I am getting this error and I feel like i’m trying to do a pretty straight forward replace/update the text of a label… I keep getting this error:
Invalid assignment of property or key ‘text’ with value of type ‘String’ on a base object of type ‘Label’.

func _on_button_pressed() → void:
var _sDice1 : String

_iDice1 = randi() % 6 + 1  # Random number between 1 and 6
_sDice1 = str(_iDice1)
Label.text = "Dice1 : %s" %_sDice1

Appreciate the help.

Label is a class, and not a reference to a specific label.
If your Label is called Label that might be an issue, as GDScript might not know if you are trying to reference the class Label or a variable with the name Label.
Make sure your label’s reference is correct, and if it is, and it’s called Label, try to rename it to something else, such as: label or _label.

2 Likes

I think you might also need a space after your % sign:

Label.text = "Dice1 : %s" % _sDice1

Not sure, but thought I would mention it.

Also you can use for clarity:

_iDice1 = randi_range(1, 6)
1 Like

I appreciate the help.

I renamed the label in the node tree to “lD1” and then added/changed the refrence in the gd script and it fail here:

extends Control

@export
var lD1 : Label
var _iDice1 : int = 1

func _ready() → void:
# Initialize random number generator
randomize()
_iDice1 = 1
→ lD1.text = “test”

with this error:" Invalid assignment of property or key ‘text’ with value of type ‘String’ on a base object of type ‘Nil’."

That means you didn’t assigned a label node for your variable in the inspector.

1 Like

@matheusmdx

Appreciate the help, what is the minimum code needed in the script to set the text on a label (named lD1)?

~Thanks, Jim

$lD1.text = “test”

Fixed it.

Thank you everyone for the help.

~Jim

You don’t need any extra code, just assign the label for your variable like in this video:

1 Like