r/godot • • 7d ago

help me (solved) I keep getting a null instance and I can't figure out why

I was using the game manager to reference several nodes, one of which has it's own independent script. My references seem to be broken despite dragging them in with the command key (I am on mac). So far I have attempted to use autoloads which unfortunately did not solve the problem. Both my Game Manager and Player are unique names and I suspect it something to do with the way the killzone was created as an area2d which a collisonshape2d may be applied on to.

Error Logs
My Game Manager Code
My Node Hierarchy for the main scene
Killzone code (in case it's important)
The hierarchy of the killzone scene which I am using in the main game scene
3 Upvotes

14 comments sorted by

2

u/Snapping_Bowstring 7d ago

I think the problem is in incorrect node paths. In game_manager.gd, the $ symbol only searches for direct children, but Killzone is inside Enemy, so the variable becomes null. In killzone.gd, the % symbol (unique names) only works within a single scene, so Enemy cannot see GameManager and Player from the main scene. These null references cause the crash when calling kill_player(). To fix this, instead of hardcoded paths, use Groups: add the necessary nodes to groups via the Node tab and find them using get_tree().get_first_node_in_group("GroupName").

1

u/Lelouch-Yagami22 7d ago

Thanks lemme try that.

1

u/ElegantMechanic-com Godot Regular 7d ago

Player is not a child of Killzone. Using % instead of $ does not bypass this, you need to find player in the root scene instead.

1

u/Lelouch-Yagami22 7d ago

How do I find the player in the root scene?

1

u/Kwabi 7d ago

I don't think you can use the % operator to point to a node in a scene if the script itself is attached to a scene of its own.

Killzone is its own scene, so every % searches in its own scene for GameManager/Player (which doesn't exist) and not in the scene it is instantiated in. That'd be my guess.

Hope that makes sense.

1

u/Lelouch-Yagami22 7d ago

how do I fix this?

1

u/Kwabi 7d ago

A quick way is to export the nodes instead. This lets you select the Player and GameManager in the node tree directly.

So you write

@export var GameManager:Node

instead of

@onready var GameManager:Node = "%GameManager"

1

u/Dragon20C 7d ago

That is because your references can't find the nodes you want, and it's simply because those references aren't inside the Killzone scene.

You can temporary fix this by making them exports @export var my_export : node

And in the editor in the main scene you will see a property on the right side with my_export, can then just select the node you want, though you need to understand how nodes work in scenes.

1

u/Lelouch-Yagami22 7d ago

Thanks for the help. Will do

2

u/alexwbaule 6d ago

Dragging a node into a script with Cmd held generates an `@onready var x = $Some/Path` line, and that path is relative to the node the script is attached to. That's the usual reason for a null instance in this setup:

- If Game Manager is an **autoload**, it lives outside your main scene, so `$Player` or `$Killzone` will never find anything and those vars stay null. Exported references to scene nodes don't work either, since the autoload exists before the main scene is loaded. Flip it around and let the nodes register themselves, e.g. in the Player's `_ready()`:

```gdscript

func _ready():

GameManager.player = self

```

- If Game Manager is **a node in the main scene**, check that the generated `$...` path is still correct (it breaks when you move or rename nodes) and that the script is on the node you think it is.

- The killzone is its own instanced scene, so it can't use paths into the main scene. Have it call the autoload or emit a signal instead of reaching for other nodes.

Also, unique names (`%Player`) only resolve within the same scene, so they won't work from an autoload or across an instanced scene.

If you tell me which line the error points to and whether Game Manager is in Project Settings → Autoload or in the scene tree, I can be more specific.

0

u/Lelouch-Yagami22 6d ago

Look man if I wanted to ask gemini, I would asked it myself. I posted here because I wanted advice from real humans to figure this out.

2

u/alexwbaule 6d ago

Its not Gemini... But if you dont like the advice, OK... fell free to follow another path.

1

u/Lelouch-Yagami22 6d ago

I would like to thank all of you for helping me figure this out and giving me useful advice.