0 votes

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
Godot version 3.3.2
in Engine by (211 points)

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

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

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

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

Im kind of new to godot

1 Answer

+1 vote
Best answer

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)
by (1,455 points)
selected by

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

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

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 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 webmaster@godotengine.org with your username.