Help with Collectable Counter

Godot Version

4.2

Question

I’ve been working on a 2D platformer game. I just added collectables and then I got stuck on adding a counter for them. I can’t figure out how to change the text on the label or how to keep it fixed to the screen and not scroll away from the camera. Can someone help me with this?

Add a CanvasLayer node as a parent of your GUI, this will ensure it stays fixed on screen. Changing the text can be done in a number of ways, it would help to know what you’ve written so far. I would add an export variable to the player, or child the CanvasLayer + GUI then create a function to update the player’s label like so

extends CharacterBody2D
class_name Player

var coins: int = 0

func add_coins(count: int) -> void:
    coins += count
    $CanvasLayer/Label.text = str(coins)

Now your coin script can call this function to update the variable and the visuals

extends Area2D

func _on_body_entered(body: Node2D) -> void:
    if body is Player:
        body.add_coins(1)
        queue_free()
2 Likes

Thank you! It’s working great.