Null reference when trying to get reference from child

Godot Version

version 4.1.2

Question

I’ve been ripping my hair out about this for several hours now. I’m trying to get a reference to my tile map from it’s parent node. But it just returns a null reference.

Main.gd is attached to “Node2D”

I’m new to Godot, so I’m not sure if I’m just trying to do something forbidden for whatever reason. I’ve been able to find nothing online about this problem either.

Just drag the TileMap node to the script while holding Ctrl. You should get the correct path in the declaration.

Didn’t fix it, unfortunately. Still the same problem

I can think of quite a few things that might be the issue. If you’re willing to zip up your project and send it my way, I’ll be happy to run a few tests and then explain what the issue is once I find it. I’m certain I can find the issue but I’m not quite where I can do this stuff in my head yet :stuck_out_tongue:

It would be easier to locate the issue source if you put your project on GitHub, so people who are willing to help can clone it and try it out locally.

GitHub would be great. I usually default to zip because I know a lot of people, especially new programmers, don’t know git and/or it would be easier for them to just zip something up with a right click. I’m fine with either.

Gotcha! Here’s a zip file. Thanks in advance!

Thanks Zane! I’ve got an appointment shortly but I’ve downloaded this zip and will have a look this afternoon!

OK, I found the issue. You’ve got Main setup as an autoload but you also have it attached to the top-level Node2D of your Test.gd. So the instance that’s returning null is that Autoload, which gets loaded first and does not have a TileMap. You can see that in the RemoteTree shown below (shows the game’s Scene Tree at runtime)

Main.gd should not be autoloaded. Go to Project > Project Settings >Autoloads (tab) and delete Main as an autoload.

There are other errors, but you can get to those next. I do not recommend using find_child() in your @onready var. These are all better alternatives in my opinion, from my favorite to least favorite:

  1. Right click on the node, select Use a Access as Unique Name, click drag, then hold control before releasing.

  1. Do it as an export var - requires manual assignment in editor. Don’t love it, but can be nice if you’re extending this Node and need to assign different things for some levels.

CleanShot 2024-02-27 at 15.52.00

  1. Lastly, you can use find_child() (something I’m assuming you’re used to doing in Uinty - I used to do that a lot in C# unity) but again it’s an unnecessary search in my opinion, and will break if you rename or move it. IF you want to do this method, I’d do it in the _ready() function in stead of an @onready because for some reason having a find in an @onready feels… icky to me :stuck_out_tongue:

Anyway, I hope that’s helpful!

1 Like

Oh man, okay cool. Thank you so much! I had it in autoload so I could use it as a global script, guess I should have researched that more before using it that way.

You got it! Happy to help.

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