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.
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?
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.
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.
@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?
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.
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()
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.