Button background color problem

Godot Version

v4.3.stable.official [77dcf97d8]

Question

Hallo , I have 2 buttons in the scene . I want when i press the button to change the background color and stays in the same color. When I press the NextButton to change to the original color again the Button. The problem is that when I press Button all the buttons in the scene change the bg color. What Im going wrong. Thanks

extends Node2D

@onready var button = $Button

func _ready():
print("Initial button color: ", button.modulate)

func _on_Button_pressed() → void:

if button.modulate != Color.GREEN:  
	var styleBox = button.get("custom_styles/normal/bg_color")
	set_stylebox_color("normal", Color.GREEN)

func _on_next_pressed() → void:
set_stylebox_color(“normal”, Color.GRAY)

func set_stylebox_color(style_box_type: String, color: Color):
var stylebox_theme: StyleBoxFlat = button.get_theme_stylebox(style_box_type)
stylebox_theme.bg_color = color
stylebox_theme.border_color = color
button.add_theme_stylebox_override(style_box_type, stylebox_theme)

I assume they all share the same stylebox and therefore every button will change its colors when you change one. If you want different behaviour you can activate “local_to_scene” to the stylebox-resource

1 Like

Thanks a lot , that worked fine.