I'm instancing a scene, using a constructor.
Within that scene, I have a script targeting a node as normal, using onready var example = $Example
.
It autofills when typing, and the child is the only thing below the root node in the hierarchy.
But when _ready() happens, the node is null.
This is a stripped-down version of the script:
extends Node2D
class_name Alien
var thisColour
var health
onready var alienSprite = $Alien_Sprite
func _init(alienColour):
thisColour = alienColour
func _ready():
match thisColour:
"beige":
alienSprite.play("beige")
health = 20
"green":
alienSprite.play("green")
health = 30
etc.
and in the main scene, it's being instanced with:
var newAlien = Alien.new("green")
add_child(newAlien)
calling print(alienSprite)
in _ready() gives [Object:null]
In the debugger, it says "Attempt to call function 'play' in base 'null instance' on a null instance".
The error goes as follows:
