+1 vote

This is less of a personal question but more of what I discovered and what could help anyone in need.

Very often I run into the can't do blank on base Null Instance which was a really big wall in my game making, but after some searching on the Godot Docs in the node section it states under the get_node() function that a null instance is where Godot cannot find a node. Using that knowledge if you run into that error you should first check to see if you're correctly calling the node (If it is a child of a node like...)


make sure you are using $Area2D/CollisionShape2D or get_node("Area2D/CollisionShape2D")
to call it correctly. That may be what fixes your errors.

I hope this knowledge is useful because I really found it handy.

in Engine by (71 points)

1 Answer

+1 vote

In the case of get_node or $SomeNode, a null instance error following that indeed means the node wasn't found. But the null instance error itself only means you are trying to call a method on a variable which contains null. For example, in obj.do_this(), if obj is null, Godot cannot call do_this(). This can happen in many other cases not involving nodes.

The debugger will stop on those null accesses because there is no recovery possible, but the downside is that the origin of that null may have run long before, which is not immediately obvious.
Functions that can return null in case of an error don't stop the debugger. However, they may log an error instead, which you can see in the console log, with the call stack attached most of the time. This is what you should look for, in case you run into a null error.
The debugger doesn't stop because the program can decide to keep running: setting a variable to null isn't preventing GDScript from working. But trying to call a method on null does.

That said... I wish there was a break on error option in Godot so that the debugger actually stops the game pointing at the origin of errors as they occur, especially if the origin is a script.

by (29,092 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.