Invalid call. Nonexistent function 'Instance' in base 'Nil' (gdscript)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Nonken
export (PackedScene) var fireball_scene
var fire_ball = fireball_scene.instance()

Something about these lines gives the error in the title. I’m really new to this so I have no idea what’s wrong or what Nil is. What I’m trying to do is “spawn” in a fireball when the player presses space.

:bust_in_silhouette: Reply From: MEDBVLL

add the ‘onready’ (‘@onready’ in 4.x) keyword in front of your instance() line, like so:

export (PackedScene) var fireball_scene
onready var fire_ball = fireball_scene.instance()

alternatively, you can also manually instance it in the _ready() function:

export (PackedScene) var fireball_scene
var fire_ball: Node

func _ready():
	fire_ball = fireball_scene.instance()

debugging should show you in your code that fireball_scene is null when attempting to call .instance()
it looks to be caused by how items in the inspector behave, they are probably loaded after the script is, but before the _ready() function is called