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)