Label not updating

Godot Version

4.2.1 Stable

Question

I need some help with this code. I made it so when you enter the body of the object, it adds 10 to your ammoCount. But when it does, and I click “shoot” it doesn’t go down. It just stays there. Here’s the code:

extends Area2D

var playerHasTouched = false
var canReload = false
var hasShot = false
var reloaded = false

static var bulletCount = 10

@onready var bullet_count = $"../Player/Camera2D/Bullet Count"

func _input(event):
    if  Input.is_action_just_pressed("Shoot") and canReload == false and hasShot == false:
            bulletCount -= 1
            hasShot = true
            await get_tree().create_timer(0.5).timeout
            hasShot = false
            
            bullet_count.text = "BULLET COUNT: " + str(bulletCount)
            
            if bulletCount == 0:
                canReload = true
                
                print("BOOL: " + str(canReload))

func _on_body_entered(body):
        playerHasTouched = true
        
        if bulletCount == 0 and canReload == true and playerHasTouched == true:
            replenish()
        elif playerHasTouched == false:
            Reload.reloadAmmo = false
            reloaded = false
        
        print("This Worked!")
        queue_free()

func replenish():
    bulletCount += 10
    bullet_count.text = "BULLET COUNT: " + str(bulletCount)
    
    Reload.reloadAmmo = true
    hasShot = false
    canReload = false
    reloaded = true
``` sorry if it is messy.

Doesn’t seem like there is an animation, how do you know it is shooting?

Three things stick out to me, but I do not think they are the issue.

  1. the _input function is to make use of it’s argument instead of the Input singleton
func _input(event: InputEvent) -> void:
    if event.is_action_pressed("Shoot") and !canReload and !hasShot:
  1. waiting half a second then updating the text is strange

I have made a global script. I attached it to the script I provided and my Player script.

The way I know it is shooting is there is a shooting function in my Player script. I have mirrored the two in the Player script so I can see the bullets go. It also prints the count.

As for the script I provided, it just supposed to update the label. The global script tells both scripts if the Player has reloaded or not. It probably sounds complex and pretty dumb.

Maybe this is a different bullet counter than you expect, or being overriden by another of the same script.

@onready var bullet_count = $"../Player/Camera2D/Bullet Count"

Sounds really bad to have a duplicate script and really really bad to have a similar static variable on each, since they won’t be shared like normal statics are.

Generally I would avoid going up the tree on ready i.e. $"../ an @export is a suitable replacement.

So would that explain why when the bulletCount goes back up to 10 it doesn’t update again?