Topic was automatically imported from the old Question2Answer platform.
Asked By
LudosGD
Hiya! I have a problem instancing “enemies” for my platformer.
I have a scene for the level and another scene for the enemy (which is a KinematicBody2D); I’m trying to use a Path2D to spawn several enemies. Here’s the code:
extends Node2D
signal restarting
export (PackedScene) var Enemy
export (int) var total_enemies = 0
# Called when the node enters the scene tree for the first time.
func _ready():
var pos_curve = $EnemyPositions.get_curve()
total_enemies = pos_curve.get_point_count()
for i in range(0, total_enemies):
var enemy = Enemy.instance()
add_child(enemy)
enemy.position = pos_curve.get_point_position(i)
enemy.startPositionX = enemy.position.x
enemy.startPositionY = enemy.position.y
enemy.directedToRight = true
When I run the code, I receive the message in the title, referred to this line:
That is telling you that Enemy has no value, and therefore instance() can’t be called on it. When you declare the variable at the top like this:
export (PackedScene) var Enemy
You haven’t given it a value. You need to select it in the Inspector (it shows up there because you used export) and assign the .tscn file you want. You can either click the Inspector field and choose “Load”, or drag the scene file from the Filesystem dock and drop it on the property in the Inspector.
Doing that is equivalent to declaring it directly like this:
var Enemy = load("res://some_scene_file.tscn")
Ok, now I modified it and it works. Thank you very much!
LudosGD | 2019-07-10 09:14
Which of those two ways of doing it is better?
andrsrzdc | 2019-07-26 04:36
Neither is “better”. It’s just two different ways of getting the same result.
I getting the same error when following you tutorial on multi target camera on the below link. I have completed the Camera script but getting the abover error on the tilemap script.
I am also new to game development and this is my first time learning any game engine I am following Coco code YouTube tutorial and unit second video he told how to include a game manager in the game to keep a track of score, I follow his guide world by word but I am still encountering this issue but in his video he doesn’t