Code can't find Nodes

Godot Version

v4.3-dev5

I don’t understand that much of godot bu for me this is non-sence it’s saying that is not finding both nodes but they are right there

collectable.gd:

extends Area2D

@onready var game_manager: Node = %GameManager

func _on_body_entered(body: Node2D) -> void:
	if (body.name == "Player"):
		game_manager.add_point()
		queue_free()

game_manager.gd:

extends Node

@onready var points_label: Label = %PointsLabel

var points = 0

func add_point():
	points += 1
	%PointsLabel.text = ("Apples: " + str(points))

Errors:
E 0:00:00:0765 collectable.gd:3 @ _ready(): Node not found: “%GameManager” (relative to “/root/Collectable”).
<C++ Error> Method/function failed. Returning: nullptr
<C++ Source> scene/main/node.cpp:1723 @ get_node()
collectable.gd:3 @ _ready()

E 0:00:00:0765 game_manager.gd:3 @ _ready(): Node not found: “%PointsLabel” (relative to “/root/GameManager”).
<C++ Error> Method/function failed. Returning: nullptr
<C++ Source> scene/main/node.cpp:1723 @ get_node()
game_manager.gd:3 @ _ready()

% only works with the “main” scene

1 Like

it easier if you remove the unique name and just drag and drop the node to your script

1 Like

still not finding anything

try $“…/UI/Panel/PointsLabel”

1 Like

Try:

$"../UI/Panel/PointsLabel"

it got worse

Could you share a .zip with the project in order to be able to reproduce this?

I don’t know if this is the way to do it, but here it is:

https://www.mediafire.com/file/0p3oe5g9ds25go2/froggy.zip/file

There are multiple things failing.

First of all, and most important. The game_manager.gd is loaded as an Autoload, which shouldn’t be for this use case. Remove the GameManager autoload by pressing the trash can at the right of this settings.

Also the node GameManager in the Main scene is loading the wrong script, remove the current script, and asign the game_manager.gd.

Once the GameManager uses the correct script, and it isn’t loaded as an autoload, use this code for the game_manager.gd.

extends Node

@onready var points_label: Label = $"../UI/Panel/PointsLabel"

var points = 0

func add_point(): 
	points += 1
	points_label.text = ("Apples: " + str(points))

my solution don’t work… sorry :wink: [deleted]

Now even the first level doesn’t work

I kinda fixed the issue but now it gives this error:


imagem

Current State:
https://www.mediafire.com/file/n0ovpb7ulrinzux/froggy.zip/file

Since GameManager is an auto load you don’t need to cache the reference.

It just

GameManager.do_a_func()

Autoloads are also siblings of the main scene it should be

$"../Main/UI/Panel/PointsLabel"

I would also advise against a reference like that from an auto load. The game manager should provide a signal that the UI panel will connect itself to.

1 Like

In Level2 you have exact same error you had in Level1, the script attached to GameManager It’s not game_manager.gd. Remove that current script, and use game_manager.gd instead.

Once I did that, the game stopped crashing for me.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.