This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
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 Engine 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 (22,191 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 Frequently asked questions and 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 [email protected] with your username.