Unable to access child nodes if storing them as variable

Godot Version

4

Question

Hi there, I’m currently trying to change the texture of a child node upon loading the scene. For reference, this is my scene structure:
image

I’ve store “TerrainHandler” and its child “Sprite2D” in the variables here:
image

Upon the scene launching, I want to set the Sprite2D child node of TerrainHandler to an art resource
image

The issue is, whenever I launch the scene, it’s pulling up this error:
image

Oddly, when I change the reference of the Sprite2D to below, it runs without issue.
image

Anyone knows how I can solve this issue, while still storing the child node as a variable (i.e. tsprite_2d)? Thanks!

Set attributes on exported values will happen after init, before ready, and you won’t get your children until ready is called. @onready

Basically you need to check if the node is in the tree before trying to update a child node.

is_inside_tree()

Because of this limitation you will also need to set your variables when the node is added to the tree during _ready.