button doesnt work when I set it to show it hides

Godot Version

godot version 4.7.2

Question

I was making a shop sistem and I wanted the buttons to buy the stuff disapear if there’s not an item in its collum (I recently started godot), this is the code I used

if a == 0:
				%Button2.hide()
				%Button.show()
				%Button3.hide()
				%Button4.hide()
				%Button5.hide()
			elif a == 1:
				%Button2.show()
				%Button3.hide()
				%Button4.hide()
				%Button5.hide()
			elif a == 2:
				%Button2.show()
				%Button3.show()
				%Button4.hide()
				%Button5.hide()
			elif a == 3:
				%Button2.show()
				%Button3.show()
				%Button4.show()
				%Button5.hide()
			elif a == 4:
				%Button2.show()
				%Button3.show()
				%Button4.show()
				%Button5.show()

it basically sees the length of the list (a) and determines if its shown or not (Im pretty sure theres a more effective way) could someone say me what to look for to why it doesnt work?

Untested, but I think I’d use the opposite ‘logic’, by showing all the buttons, then…

match a:

0:
%Button2.hide()
%Button3.hide()
%Button4.hide()
%Button5.hide()

1:
%Button3.hide()
%Button4.hide()
%Button5.hide()

2:
%Button4.hide()
%Button5.hide()

3:
%Button5.hide()

Sorry about the lack of indentation; the ‘format’ button doesn’t work as it should, I think.

Not sure I understand the problem scope, could you explain what you expect to happen versus what really happens? The code looks valid so I can’t gleam anything wrong with it.

If you only want to program this more efficient, you can store the buttons in an array and show/hide them while progressing through the array.

@onready var buttons: Array = [
	%Button,
	%Button2,
	%Button3,
	%Button4,
	%Button5,
]

func show_up_to(a: int) -> void:
	for i in buttons.size():
		buttons[i].visible = i < a

Thanks, I still dont know why my code didnt show the buttons strangely yours worked