Error "Attempt to call function 'instance' in base 'null instance' on a null instance."

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

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

enemy.gd

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():
	randomize()
	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):
	frames+=1
	
	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:
		queue_free()

enemy_group.gd

extends Node2D

export (PackedScene) var enemy

func _ready():
	pass

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

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?

:bust_in_silhouette: Reply From: kidscancode

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?

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

Wagner28 | 2019-07-30 13:43