Coin counter label displaying "Nil"

Godot Version



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!

it might be because the duplicated coin can’t find the “gem label” node

1 Like

I actually didn’t think of this before, but since I’m exporting the gem like so: @export var gem: Label and i have multiple duplicated gems, i have to assign the Label node to every single one of them.

Problem solved now, thanks :+1: