Exported Properties Are of Type “Nil” When Calling “add_child”

Godot Version

Godot 4.3

Question

I have a project where I have a class “Child” , a Node2D with a Sprite2D and Texture as exported properties. Here’s the code:

extends Node2D

class_name Child

@export var sprite_2d: Sprite2D
@export var default_texture: Texture

func _ready() -> void:
	
	sprite_2d.texture = default_texture

When I attempt to run this scene by itself everything is fine, however when I create an instance of Child and attempt to call “add_child”, the debugger says that the exported properties for Child have not been set. Here’s the code for that:

extends ColorRect

class_name Parent

@export var child: Child

func _ready() -> void:
	child = Child.new()
	add_child(child)

(Note that Parent also has exported property storing a reference to Child)

Also here’s a screenshot of the error I’m getting:

Can anyone help me figure out why this is happening?

I am pretty sure it is because export variables are set for the scene not the class. So you are instantiating via class whereas the export variables are set and saved via the scene. (I could be wrong) have you tried rather then calling Child.new() loading and instantiating the scene itself to see if it works.

Also, have you declared the export values?

2 Likes