Error when changing label's text to display current score (Brackeys YouTube Video Tutorial)

Godot Version

4.6

Question

I am following a wonderful YouTube video by Brackeys called, "How to make a Video Game - Godot Beginner Tutorial." I have been stuck for hours trying to debug the game after completing his code for the Game Manager at 1:07:17. When I collect a coin, the following errors appears:

"Invalid assignment of property or key ‘text’ with value of type ‘String’ on a base object of type ‘null instance’.”

and

E 0:00:00:996 game_manager.gd:5 @ @implicit_ready(): Node not found: “ScoreLabel” (relative to “/root/GameManager”).

I am extremely new to coding. Any help in learning how to fix this is greatly appreciated.

Here is my code for the Game Manager:

extends Node

var score: int = 0

@onready var score_label: Label = $ScoreLabel


func add_point():
	score += 1
	score_label.text = "You collected " + str(score) + " coins."

Here is my code for the coins:

extends Area2D



func _on_body_entered(body: Node2D):
	GameManager.add_point()
	queue_free()

Your GameManager doesn’t find the ScoreLabel.
Check your scene tree: Is the label a child of GameManager? Is it named correctly (“ScoreLabel”)?

Hello, thank you so much for your reply. I do have the ScoreLabel under GameManager. Was I supposed to put the GameManager node as a Control node instead? Brackeys used a normal node, but that was a slightly older version. Also, in my research on this some people seem to recommend that I do not make the GameManager script an Autoload under Globals. However, the reason I did make it autoload was because the Coin script was having trouble finding the GameManager node. Thank you for your time

I just tried using a Control Node underneath the GameManager node. I am not sure if I am using the node properly, but it did not solve the problem.

The problem is the Autoload.
When setting a script as an Autoload, it creates an additional node with that script attached. This additional GameManager node doesn’t have the ScoreLabel as child, but this is the one your coin is calling.

What exactly was the issue when trying it without the Autoload?

1 Like

Oh my goodness! Thank you for explaining that to me. It completely fixed the issue. I read somewhere to make the game manager script autoload to make the variables within global. So many hours wasted because of that. Thank you thank you

label.text = str(your string)