Cant call a function from another script

Godot Version

4.2.1

Question

hi: so i have an problem. and i cant call a function made by me. heres the script where i want to call it:

extends Area2D

@onready var game_manager = %GameManager

func _on_body_entered(body):
	if (body.name == "CharacterBody2D"):
		game_manager.add_point()
		queue_free()

and heres the script thats the function in:

extends Node

var points = 0

func add_point():
	points += 1
	print(points)

Thanks for helping!

I do know what tutorial this is in. I don’t know what is wrong but if you look back maybe something went wrong with how you wrote it? Can you tell us what the error is saying?

Is GameManager an auto-load, or unique node?

If it is the former, you shouldn’t use %, just reference it by name.

If it is the latter, make sure your node really is unique.

What do you mean by “can’t call a function”? Is there an error in runtime? If yes, what error?

The most typical scenario is that game_manager isn’t a valid reference to a node because the path %GameManager isn’t unique.

Are you sure that the condition:
if (body.name == “CharacterBody2D”):
is true ?
Try by printing something to be sure. Or put a breakpoint.

1 Like

I think luismatricardi is correct.
Unless you have specifically named your node “CharacterBody2D” you won’t get that as a name.
You can try

if (body is CharacterBody2D):     

to test if the node type matches.

1 Like