Godot Version
Godot_v4.2.2-stable_win64.exe
Question
I’m making a small 2D game & making some progress, but I have a problem with displaying “Coin Counter” using the Label
node.
I attached an Area2D
node to the coin and whenever it detects a body
entered, it should reflect on the coin counter, the code is as follow:
extends StaticBody2D
@export var gem: Label
var gem_score = 0
func _on_body_detector_body_entered(body):
print("+1 coin")
gem_score += 1
gem.text = "Coin: " + str(gem_score)
But it only works when it detect body entered on the first coin, when a body enter another coin, the game hangs and displays this error: Invalid get index 'text' (on base: 'Nil')
.
Check the below video to see what I’m talking about:
Does anyone know why the game only detects body entered only once and display an error on the Label
node? Why is my Label
node becoming null
on second body enter? Thanks!