How do I solve this strange "null instance" error( please read details)?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By AudioBellum

Okay, so I have this error that’s driving me nuts as it’s a common error and yet I don’t know how to fix it. It goes something like this:

Attempt to call function 'get_children' in base 'null instance' on a null instance.

Now typically, an error like this means that I can’t get a node properly due to a typo but, I don’t think that’s the case. The IDE told me that the error because with this line of code:

for r in shotgun_blast.get_children():
	r.cast_to.x = rand_range(spread,-spread)
	r.cast_to.y = rand_range(spread,-spread)

However, this line of code only had an error my ready(): function which looks like this in:

func _ready():
	max_ammo = ammo
	randomize()
	for r in shotgun_blast.get_children():
		r.cast_to.x = rand_range(spread,-spread)
		r.cast_to.y = rand_range(spread,-spread)

Elsewhere, this code runs perfectly well like when I used it in the firing_shotgun():

func firing_shotgun():
	if Input.is_action_pressed("fire") and not ammo <= 0:
		if using == false:
			using = true
			ammo -= 1
			anim_player.playback_speed = firing_speed
			anim_player.play(firing_anim)
			for r in shotgun_blast.get_children():
				r.cast_to.x = rand_range(spread,-spread)
				r.cast_to.y = rand_range(spread,-spread)
				if r.is_colliding():
					bullet_holes(r)
					if r.get_collider().is_in_group("enemy"):
						r.get_collider().health -= 50
			yield(anim_player,"animation_finished")
			using = false
	elif using == false and not anim_player.is_playing():
		anim_player.playback_speed = 1
		anim_player.play("idle")
	ammo_count.text = "Ammo:" + String(ammo)

The fact that the error only shows up in the Ready() function and firing_shotgun() implies that I do have proper access to the node shotgun_blast. Could you please him me?

If your scene misses a resource then it won’t be properly loaded

Wakatta | 2021-12-19 11:37

:bust_in_silhouette: Reply From: Inces

This implies that Your node shotgun blast is not ready at the moment of main script ready(). What do You do with this variable shotgun_blast ? Is it referencing one node entire time or different nodes being created on the go ? DO you introduce shotgunblast as “onready” ?

I did introduce shotgunblast with onready var at the beginning of the script. Could that be the problem?

AudioBellum | 2021-12-19 12:23

No, it is good.
So there must be an issue with replacing the reference to shotgun blast. These two piecies of code - with ready() and firingshotgun() - do they come from single script ?
If Yes, show me the code of instancing and queuing free shotgun blast.
If no - we will need to take a look at both scripts, where variables are introduced and their scene_tree scheme.

Inces | 2021-12-19 13:37

“These two piecies of code - with ready() and firingshotgun() - do they come from single script ?”

Yes.

“If Yes, show me the code of instancing and queuing free shotgun blast.”

I’m not sure if I’m understanding this correctly. I don’t have a code for that. All I did was get the node for the “shotgun blast” at the beginning of the script. It’s actually a spatial node filled with a bunch of raycasts. I was following a Garbaj Youtube tutorial on the subject entitled “Shogun(Godot Tutorial)”, here is a link to it if it helps:

https://www.youtube.com/watch?v=9spikY2VhbY

AudioBellum | 2021-12-19 15:01

This is really weird. It totally sounds like typo or mistake in scene hierarchy, but nothing looks wrong here. Can you printscreen your scene tree hierarchy ?

Also, are You sure other parts of code work well ? Like shotgun actually shots, and it works when ready() function is disabled ?

Inces | 2021-12-19 18:30

Imgur: The magic of the Internet

AudioBellum | 2021-12-19 20:31

And You introduce it as:

onready var shotgun_blast = $head/Camera/shotgun_blast

?

This should be like this. If You really properly introduced this variable, like above, then this is some kind of madness :slight_smile: You might still try to debug it by referencing the node directly, not using shortcuts, so replace all shotgun_blasts with above hardcode path.

Edit : I notice head is big caps in tutorial, your scene has small :wink: Didn;t You mess it up ? :wink:

Inces | 2021-12-19 21:21

I actually introduce it like this:

onready var shotgun_blast = $“…/head/Camera/shotgun_blast”

The reason why I do this is because all the code is for a weapon script that I want to be reuseable when making any other type of weapons; I could just extend it to other scripts. Here is the other half of the node structure:

Imgur: The magic of the Internet

The bass script is attached to the Gun Node. What confuses me the most is that I have all sort of code for different type of weapons(knife,pistol,shotgun, etc) and all the others are working just fine. However, only half of the shotgun code seems to work. I’m confused.

AudioBellum | 2021-12-19 21:38

I don’t think that I need to name all the variable the same way it was named in the tutorial.

AudioBellum | 2021-12-19 22:06

No, This is incorrect reference. It does not lead to shotgun_blast at all. I don’t see how are you going to reuse it like this. This is “hardcode” reference and it is never elastic.
“…” before “head” indice get_parent(). So you are trying to call get_parent().get_node(“head”), which does not exist. Try pasting my reference and see if it works. The reason error only shows up in ready() is because it doesn’t have a chance to reach later parts of code.

Inces | 2021-12-20 08:22