0 votes

I'm having trouble on trying to instance an enemy object. The enemy script and the enemy_group script are below


extends KinematicBody2D

var SPEED = 300
var mov = Vector2(0, 0)
var frames = 0
var maxFrames

func maxFrames_():
    var nums = [30,60, 90] #list to choose from
    return nums[randi() % nums.size()]

func _ready():
    maxFrames = maxFrames_()
    position.x = 730
    position.y = int(rand_range(100, 700))
    SPEED = int(rand_range(200, 400))

# warning-ignore:unused_argument
func _process(delta):

    if frames == maxFrames:
        get_tree().call_group("node_enemy_group", "enemy_generator")
        frames = 0

    mov.x = -SPEED

# warning-ignore:return_value_discarded
    move_and_slide(mov, Vector2(0, 0))
    if position.x < -40:


extends Node2D

export (PackedScene) var enemy

func _ready():

func enemy_generator():
    var new_enemy = enemy.instance()


I added a node group in the node that will control the generation of new enemies. I've already check if the node group name is correct and I don't know what to do. Can someone help me?

in Projects by (20 points)

1 Answer

0 votes
Best answer

You didn't say where the error was located, so I can only assume it's on this line:

var new_enemy = enemy.instance()

Based on your code, the variable enemy is not assigned a value. Stating

export (PackedScene) var enemy

exposes the enemy property to the Inspector, where it must be assigned a value. Have you checked there to ensure it is not null?

by (18,211 points)
selected by

I found the mistake! I was following the steps I saw in a course I've bought and now it's working, thanks!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.