shooting, once again :)

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

hi, once again im trying to shoot, i instance the bullet scene ive made at my muzzle, but this shows me following error:

attempt to call function “add_child” in base “null instance” on a null instance.

my script:

func _physics_process(delta: float) -> void:

	if Input.is_action_just_pressed("mouseclick"):
		var b = bullet.instance()
		muzzle.add_child(b) <------- error
		b.look_at(raicast.get_collision_point(), Vector3.UP)
		
pass

i think that if you read the error carefully you can understand where the issue might come from :slight_smile:

Andrea | 2021-08-29 12:48

i probably could, but i have no clue what a null instance is and what i should change xD

theMX89 | 2021-08-29 16:09

A null instance is literally nothing, it is an empty object.
The debugger is telling that you are trying to call the method add_child, from an an empty object.
Since in your code you wrote muzzle.add_child(), it means that muzzle is empty (=null)
Either you forgot to assign a node to it, or the assignment failed

Andrea | 2021-08-29 17:04

So I have to assign a childnode to it? If not, the muzzle ist a spatial and the bullet is a rigidbody

theMX89 | 2021-08-29 18:35

Im kind of new to godot

theMX89 | 2021-08-29 18:36

:bust_in_silhouette: Reply From: Andrea

muzzle.add_child(b)

Means that Godot will fetch the content of the variable muzzle(which is supposed to be a node) and will call the function add_child from it.
But, if you never told Godot what is the content of the muzzle, Godot does not know what to do, and will tell you that you are trying to call add_child on nothing.

You can assign a node to muzzle in many different ways, depending which node is referring to:

Muzzle=$NameOfChildNode
Muzzle=get_parent()
Muzzle=get_node(path)
Muzzle=get_child(n)

thanks! it finnaly works, the bullet flys into the ground, but i can fix that.

theMX89 | 2021-08-30 08:14

fixt it :smiley: (i have to write more then 12 characters so im doing this right here)

    

theMX89 | 2021-10-07 19:07