Instancing scenes in Godot

Godot Version

godot-4

Question

I’m working on an fps game and am working on a way to more easily populate the level with enemies. I have a script attatched to a node 3d, which is the parent of several node 3ds placed around the level in place of enemies. I’d like to instance my enemy scene at the position of these children, but godot is giving me the following error:

Invalid call. Nonexistent function ‘instance’ in base ‘PackedScene’.

here’s my code, I’m not sure what exactly is causing this error.

extends Node3D


var Enemies:Array[Node3D]
@export var Target = preload("res://TestTarget.tscn")

# Called when the node enters the scene tree for the first time.
func _ready():
	for children in get_children():
		if children.name.contains("Target"):
			var target_instance = Target.instance()
			target_instance.position = children.position



# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass

instance was replaced by instantiate in 4.0, maybe you are reading old code.

it was a combination of that and the fact that I need to do the addchild thing so it was actually in the scene tree

1 Like