Help needed transitioning enemy scene into object,and back into enemy scene

Godot Version

4.5

Question

I need help changing an enemy scene into an object scene and back into the original scene

I,ve been working on puting together a prototype where the player hits enemies to change them into objects, and is able to hit them back into enemies if needed.I’ve been able to put together a hitbox and hurtbox system that works by working off some tutorials on youtube, however, it seems as though the hitbox hits it infinitly leading to a “Cannot call method ‘instantiate’ on a null value.” error.

class_name Hurtbox
extends Area2D

@export_subgroup(“Transformation”)
@export var target:Node2D
@export var transformation : PackedScene
@export var hurtbox_lifetime : float =0.05

func _process(delta: float) → void:
if hurtbox_lifetime > 0.0:
var new_timer = Timer.new()
add_child(new_timer)
new_timer.timeout.connect(add_child)
new_timer.call_deferred(“start” , hurtbox_lifetime)

func _ready() → void:
monitoring = false

set_collision_layer_value(1,false)
set_collision_layer_value(2,true)

func receive_hit(_target) ->void:
print(“I’m Hit!!”)
var instance = transformation.instantiate()
target.add_sibling(instance)
target.remove_child(target)
class_name Hitbox
extends Area2D
@export_subgroup(“Properties”)
@export var hitbox_lifetime:float
@export var shape: Shape2D

func _init(_hitbox_lifetime:float, _shape:Shape2D ):
hitbox_lifetime=_hitbox_lifetime
shape=_shape

func _ready() → void:
monitorable = false
area_entered.connect(_on_area_entered)

if hitbox_lifetime > 0.0:
	var new_timer = Timer.new()
	add_child(new_timer)
	new_timer.timeout.connect(queue_free)
	new_timer.call_deferred("start" , hitbox_lifetime)

if shape:
	var collision_shape = CollisionShape2D.new()
	collision_shape.shape = shape
	add_child(collision_shape)

set_collision_layer_value(1,false)
set_collision_mask_value(1,false)
set_collision_mask_value(2,true)

func _on_area_entered(area: Area2D) → void:
if not area.has_method(“receive_hit”):
return

area.receive_hit(null)

Any help is greatly appreciated.

Did you assign a packed scene resource to transformation property in the inspector?

yes,I did from both enemy to object and object back to enemy

Which line causes the error?

On the object hurtbox.

Sadly @exported packed scenes can cause a cyclical reference error, this silently fails after loading the first time. Try exporting the filepath instead with @export_file("*.tscn") var instance_path: String, then loading it before instantiating.

I tried but it gave me an error “Cannot find member “instantiate” in base “String”.”

You must load it first

var transformation = load(instance_path)
var instance = transformation.instantiate()

That has worked, though now the object spawns and despawns within a frame, though thanks a bunch that was a huge help!