Enemy Portal

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ThreeSpark

I am trying to make it so that an enemy spawns in a portal. I have a position2D in place, but I can’t figure out how to get the enemy to spawn on the position2D. Any suggestions?
Here is my code:

extends Node2D

onready var slime = get_node("../Slime")

func _ready():
	$AnimationPlayer.play("animation")

func _on_SlimeTimer_timeout():
	add_child(slime)
:bust_in_silhouette: Reply From: rk3Omega

Assuming your issue is instancing, you’ll need to instantiate a PackedScene then add it like so:

const Slime = preload("res://Slime.tscn")

func _on_SlimeTimer_timeout():
	var slime = Slime.instance()
	add_child(slime)

or if your issue is positioning because you’re trying to move a node from one parent to another, you’ll first need to unset its original parent, add it, and then reset it’s position:

func _on_SlimeTimer_timeout():
	var slime = get_node("../Slime")
	slime.get_parent().remove_child(slime)
	add_child(slime)
	slime.position = Vector2()

I tried that, but it is giving me an error: "Attempt to call function ‘get_parent’ in base ‘null instance’ on a null instance

ThreeSpark | 2018-08-02 02:16