Button text is not being assigned properly

Godot Version

4.6.1.stable

Question

I’m trying to use a key in a dictionary as text of a button. However, I keep getting a formatting error. I used the ‘print’ statement to verify if the right key is being assigned to the variable and it is.

extends Control

@onready var button_1: Button = $HBoxContainer/Button1
@onready var button_2: Button = $HBoxContainer/Button2
@onready var button_3: Button = $HBoxContainer/Button3
@onready var h_box_container: HBoxContainer = $HBoxContainer

var upgrade_buttons = [button_1, button_2, button_3]
var texture: AtlasTexture
var pick_die

func populate_buttons():
	for i in range (0, h_box_container.get_child_count()):
		pick_die = Globals.types_of_die.keys().pick_random()
		print(pick_die) #<--- Used for debugging
		upgrade_buttons[i].text = str(pick_die) #<--- populate_buttons: Invalid assignment of property or key 'text' with value of type 'String' on a base object of type 'Nil'.
		texture.region = Rect2(Globals.types_of_die[pick_die][0], Globals.types_of_die[pick_die][1], 16, 16)

My dictionary is in my Globals script:

var types_of_die = {
	"Plastic D2": [0, 64],
	"Plastic D3": [0, 48],
	"Plastic D4": [0, 32],
	"Plastic D5": [0, 16],
	"Plastic D6": [0, 0],
	"Gold D2": [0, 144],
	"Gold D3": [0, 128],
	"Gold D4": [0, 112],
	"Gold D5": [0, 96],
	"Gold D6": [0, 80],
	"Diamond D2": [0, 224],
	"Diamond D3": [0, 208],
	"Diamond D4": [0, 192],
	"Diamond D5": [0, 176],
	"Diamond D6": [0, 160],
}
1 Like

Do “for i in h_box_container.get_children()”. In your example I is just the index of the node, not the node itself.

1 Like

Thank solved it, thanks!

1 Like