How can I change the value of a variable from another script?
I have two scripts named Player.gd and Button.gd. I’m trying to change the value of the variable health from the Player script by pressing the button.
This is what i wrote:
Player.gd:
extends Sprite2D
var health = 4
Button.gd:
extends Button
var player = preload("res://Player.gd")
func _pressed():
player = player.new()
player.health += 1
The button functions normally but the value doesn’t change at all.
You are making a new instance of a script in the player var. What you want to do, I am guessing, is to reach the player object (on some other node - which has player.gd attached to it). You would use node path references for this.