Enemy won't create projectile when it spawns through a spawner

Godot Version

Enemy:

var target
var proj : PackedScene = preload("res://Enemies/Flying Enemy/flying_proj.tscn")

@onready var stun = $Stun
@onready var turntimer = $Turn
@onready var throw = $Throw

enum States {
	Still,
	Move,
	Turn,
}

func checkToThrow():
	var dC = target.global_position - position
	if position.y > target.global_position.y - 20 and position.y < target.global_position.y + 40 and round(dC.normalized().x) == direction:
		var projectile = proj.instantiate()
		owner.add_child(projectile)
		projectile.direction = direction
		projectile.transform = transform

Spawner:

@export var enemy : PackedScene
var active : bool = false

var C_enemy

@onready var cooldown = $Cooldown

func _on_visible_on_screen_notifier_2d_screen_entered():
	active = true
	
	if active == true:
		if enemy != null and C_enemy == null:
			
			C_enemy = enemy.instantiate()
			C_enemy.position = global_position
			owner.add_child(C_enemy)

func _on_visible_on_screen_notifier_2d_screen_exited():
	active = false

func _on_cooldown_timeout():
	if active == true:
		if enemy != null and C_enemy == null:
			
			C_enemy = enemy.instantiate()
			C_enemy.position = global_position
			owner.add_child(C_enemy)

Question

When I place in the the enemy normally it can spawn a projectile, but when it is spawned through a spawner it says "cannot call method add_child on a null value".

owner is a property better suited for in-editor tasks like plugins, Maybe you should use self.add_sibling instead

2 Likes

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