Getting "null" errors but the path is correct and used identically in other places

Godot Version

4.3

Question

Hello! I am getting the error “Invalid access to property or key ‘plantselected’ on a base object of type ‘null instance’.”, but I’ve quintuple-checked the paths and tried several different methods and I really, genuinely think it is correct, so I don’t understand where it’s coming from.

Here I’m using $Game. That should work - firstly, I’m following a tutorial that does it that way, and it worked for that person. Admittedly they were using a past version of Godot, but secondly, I’ve used it before.

Just to be sure, I also tried this method:


Which I know works, because I use it identically in this script:

But that doesn’t work either. It does change the error to “Invalid access to property or key ‘plantselected’ on a base object of type ‘Nil’.”, but that doesn’t really help me much.

I am, as you can probably guess, rather new to coding, and I just don’t understand why this is happening. I apologize if this is the same ground a lot of other threads have tread, but I couldn’t find a solution after a lot of googling, so I thought I’d just ask. Thanks in advance.

Hi @aitze , Could you please try run your game and while the game is running, check the “Remote” tab of “Scene” tab and see what nodes do you have? Is Game node there?

1 Like

Can you try the folloeing instead:

@onready var plant = $Game.plantselected

Thank you for the quick response! I have, but it causes the same null instance error.

Hello nines! Thank you for the quick response. I can’t run the game as is, because it crashes on startup with the null error. However, if I comment out the offending script and run the game, I do indeed have the Game node there, with all other nodes - including this one - as children of it.

We need to see the scene tree then. Especially where is that Game node in relation with the node that is trying to access it during gameplay.

Even if it crashes you should be able to hit Remote and see the scene tree at the moment of the crash.

Sure, here you go. the Game node is above, everything under it are its children. the node trying to access it is growingZone, all the way at the bottom. It is a direct child of Game.

I see! In that case, here is the scene tree in Remote at the time of crash:

You are correct that Game is not in there - I guess it crashes before it can access it, but I wouldn’t understand why.

@aitze Where did you instantiate and add_child the game node? Perhaps you may need to tell us what happens first, second, third, etc. What is the first scene of your game and how is the game node come into this picture?

Are you sure Game is not a child of QuestSystem?

Quite sure!

I just managed to get it to work with this solution, as directed by a friend:

I don’t understand why this worked and the others didn’t - it feels to me like they should have worked - but, hey, progress is progress!

I had Game before I ever installed the questsystem plugin, and haven’t touched that yet.

Is because you where looking for the itemselected member of Game before Game was ready.

1 Like

The Game node is the very first node I added when I made this project. I think I made a 2d scene and then added that, and everything since then has been added to it.

You need to use $'..'.plantselected

This two dots point to the parent relative to the node running the script on hierarchy. They are like the two dots used in cd command on linux terminal

Btw, if you use this variable in various points, consider instantiate it on the beginning of your script with @onready var game = $'..' because every time that you use a reference with $ godot runs get_node(), and by holding the reference on a variable when the script starts you avoid doing this extra task and gain more performance.

As you was trying to get the reference to the parent of the node that you are using, you could just simply use get_parent()

1 Like

get_node("/root/Game") works because you are getting the absolute path for the node that you want. When you use $ you get the relative path and it’s relative to the node that is running the script.

1 Like

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