I cant create a Global score count for the coins I pick up

Godot Version

Godot 4

Question

Hi, i started making a game for a school project, but i cannot get over this on problem ive been stuck on for over 3 hours. If you could help, i’d appreciate it greatly.
My problem is trying to implement a global score system, but it keeps giving errors like: ‘cant call a function in base ‘null instance’ on a null instance’

I tried ChatGPT, YT videos, but nothing works.

This is the code for the Area2D of my collectible (coin):

extends Area2D

@onready var game_manager = $“…/Game manager” # Adjust the path as necessary

func _ready():
connect(“body_entered”, Callable(self, “_on_body_entered”))

func _on_body_entered(body: PhysicsBody2D) → void:
if body.is_in_group(“player”): # Using group instead of name for flexibility
queue_free()
game_manager.add_point()

This is for the Game manager I use in my 2 scenes/levels:

extends Node

@onready var count = %Count

var point = 0

func add_point():
point += 1
print(point)
count.text = str(point)

AND this is the Global.gd (I did make an autoload so thats not the problem. I just need code that works.:

extends Node

var point = 0

@onready var count = %Count

func add_point():
point += 1
print(point)
count.text = str(point)

I tried replicating your issue, and from the code you’ve supplied it looks like you don’t use the Global.gd code at all to add_point (you are calling add_point through the game_manager and it doesn’t call the Global.add_point) so there is still an issue of globals initializing before the %Count so it doesn’t have access to it, but it wouldn’t crash the game since you’re never running this code.

So without the precise point at which the error is being thrown it’s really hard to debug what is going on there. You should try to create a minimal replicable example for us to try ourselves because it’s really hard to just predict what is wrong with your code with such little information.

If I had to guess I’d say it’s that the .../game_manager line has something to do with it, but it’s just a wild guess. Have you tried adding a group to the game_manager and getting it with

@onready var game_manager = get_tree().get_first_node_in_group("game_manager")

?