call function from other script, PLS help I'm new

godot 4.4.1

I try to make a score mechanic in my game and I have 2 codes for it:

extends Node

var score = 0

@onready var ScoreLabel: Label = $GameManager/ScoreLabel

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

and:
extends Area2D

@onready var game_manager: Node = %GameManager

func _on_body_entered(_body: Node2D) → void:
game_manager.add_point()
queue_free()

IDK why, but the add_point function isn’t visible for the other function.
when I touch a coin the game freezes and I have to try again.
when I touch a coin this error pops out:
Invalid call. Nonexistent function ‘add_point’ in base ‘Node’

Here you declare game_manager to be of type Node. Only the default Node functions (and technically any functions that the Node class inherits from its parent classes) are available in game_manager.

The script you created for updating the label extends Node. i.e. it takes the base class Node and adds extra functionality to it. If you want other scripts to access the method you added, you need to specify a class_name. Like this:

And in your other script:

edit: when you post code, please add three backticks ('```) before and after your code. This keeps formatting intact.

2 Likes