Invalid call. Nonexistent function 'instance' in base 'Nil'.

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

Hi there. I’m attempting to export this hitbox variable so that I don’t have to hard code the path. This is the code that’s throwing the error:

export(PackedScene) onready var hitbox
var temp_hitbox = hitbox.instance()

This is the code that works:

export var hitbox = preload("res://Entity/Hitbox.tscn")
var temp_hitbox = hitbox.instance()

I’ve made sure to select the scene in the editor. I don’t see any other reason this error would occur. Am I missing something?

:bust_in_silhouette: Reply From: Legorel

In your first code

export(PackedScene) onready var hitbox
var temp_hitbox = hitbox.instance()

The variable hitbox is marked as onready, this is equal to creating the variable inside the _ready function. The variable temp_hitbox isn’t marked as onready, this means it’s intitialized before hitbox.
To fix the problem you need to set the variable temp_hitbox as onready like this:

export(PackedScene) onready var hitbox
onready var temp_hitbox = hitbox.instance()

Thank you. I spent a few hours on this. Must have overlooked that. Haha.

Hammo | 2021-04-12 21:44