Texturect Node nullified after ready

Godot 4.4

I’m having a strange behavior on one of my scripts, this is the script:

extends Control

@onready var icon : TextureRect = $ItemIcon
@onready var item_name : Label = $ItemName
@onready var time_to_craft : Label = $TimeToCraft

func _ready() -> void:
	print("1: ",icon)

func init_node(_ico: CompressedTexture2D, it_name: String, tim_craft: float) -> void:
	print(icon)
	icon.texture = _ico
	item_name.text = it_name
	time_to_craft.text = str(tim_craft) + " Seconds"

Which gives me the error on runtime:

Invalid assignment of property or key ‘texture’ with value of type ‘CompressedTexture2D’ on a base object of type ‘Nil’.

So the error being, when I try to set the texture to the node, it complains about it being nil. To investigate this, I put the printing there, and as expected (As well as unexpected), the output was:

1: ItemIcon:<TextureRect#58518931328>
<null>

Is there a particular reason I’m missing this node suddenly becomes null? I tried setting manually a placeholder texture to it to make sure it has something to it’s texture parameter, but nothing happened.

Where do you run your init_node() from? Can you paste this part of code?

1 Like

You made me think about it and check the sequence of events haha, thanks!

So the init_node function is called as a signal from the loading the scene script. The thing is, I’m creating this Nodes in UI dinamically, and instancing them around. The issue was, I wasn’t adding the Node to the scene with add_child(). facepalm

Thanks, case closed! I was going crazy over this for the last couple hours.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.