Can not call methods on a null value

Godot Version

4.3

Question

Idk why but this error was not there like yesterday or something but when I test it again today this error come up like wtf. Its say cant call methods on a null value but ITS not a null value, its was working well yesterday and now this?? wtf. Please help me with this.

@onready var e: TextureRect = $"Tom-Path/Tom-follow/Tom/E"
func _ready() -> void:
	e.hide()

The path is not wrong!
Screenshot (62)
Please help meee. Thanks you so muchh!

What happens if you print e?

func _ready() -> void:
	print(e)

<Object#null>

Is this being instantiated? Or it is extending a custom class? Is it itself a base class?

(Just trying to identify why it might be going wrong)

Sorry I need to get on with my own game so will come back to this.

In the meantime just move it into the ready function should solve it.

var e: TextureRect

func _ready() -> void:
	e = $"Tom-Path/Tom-follow/Tom/E"
	e.hide()

If you are having problems because your are instantiating this node, also try using the complete root path.

e = get_node("/root/PathToYourScene/Tom-Path/Tom-follow/Tom/E")

none of that work its still show the same error

If it is any of these, then there are nuances to the @ready and when it is called.

i dont really know what you mean but I just take that node and slide it in the script. The script is in the same tree as the “e” node

btw the error showing up after I make the script Globals in the project setting.

So if the script is a global, then the @ready function is called before any other nodes in the scene tree are ready.

If you really have to do that (and I don’t know why you would be doing this) you should use a call to a setter function from the scene tree into the global.

BTW, classes are super handy. Have a read in the docs about custom classes. Perhaps this would be better in your case than a global script.

And here you can read about when autoloads are loaded (which affects the @onready for globals):

so what can I do to fix it?

Without much more information like what your global is for, how you are calling it, what you are trying to achieve, the best answer I can give you is that a global @ready is called too early to reference a node in the tree. My advice, since you appear to be using this attached to yet another node (which is not what globals are used for) is that you might be looking for is to create a custom class, but who knows what you are trying to achieve here? Why is a script that is registered as global being attached to a node?

At least we know why your @ready is showing e as null. Which was the original question. And you have links to the docs about globals that explains why this is the case (for very good reasons I might add).

How about you reframe the question to something like, “I want to achieve this with this node in this script to do this but I don’t know how” and perhaps I can be of more help.

If anyone else can chime in here to help clarify this I would be grateful.

I hope this has helped in some way.

I make the script global is for dialouge manager addon for global variable

But thanksyou now I understand why its doesnt work.

1 Like

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