Keeps repating cannot call method add_child on a null value

Godot Version

extends Area2D
class_name EnemyBullet

@export var speed = 5
@export var bullet : PackedScene

signal toggle_enemy_hit(is_hit : bool)

var enemy_hit : bool = false:
get:
return enemy_hit
set(value):
enemy_hit=value

	emit_signal("toggle_enemy_hit", enemy_hit)

var direction := Vector2.ZERO
signal bullet_hit

Called when the node enters the scene tree for the first time.

func _ready():
pass # Replace with function body.

Called every frame. ‘delta’ is the elapsed time since the previous frame.

func _physics_process(_Delta):
if direction != Vector2.ZERO:
var velocity = direction * speed
global_position += velocity

func set_direction(direction: Vector2):
self.direction = direction

func _on_body_entered(body):
print(“hit player”)
if body.is_in_group(“player”):
bullet_hit.emit(body)
body.hide()
queue_free()
#body.queue_free()

Question

trying to set up enemy bullet and after shooting one bullet game crashes and gives the cannot call method 'add_child" on a null value.

1 Like

Please don’t just dump code without context or explanation. It makes things very difficult and most people will just skip and move on. Also, if you have to put code, use the </> button above to format it properly.

1st - The line generating the error is NOT in that code dump.
2nd - Find the line that makes the crash. it will look something like this:

someNode.add_child(someChild)

The error means that “someNode” is not initialized, thus null.