TextureRect Node not found, even when the node hierarchy is correct. godot 3.5.3

Godot Version

3.5.3

Question

Hi, Im quite new to Godot and have been following this tutorial (which I’m sure many of you are familiar with) online to make an inventory: https://www.youtube.com/watch?v=ssYqIQQPt7A&t=934s&ab_channel=Arkeve

However, under my Item 2D node, it is not recognising the TextureRect and Label as children. I have the hierarchy set up correctly with the script attached to the Item node and both the Label and the TextureRect are children of this node. I have modified the script from the video to try and figure out the problem, which occurs whenever I try to open the inventory:

extends Node2D

onready var item_texture = $TextureRect
var item_name
var item_quantity
var prisma2 = null
var being_obtained = false

func _ready():
	item_name = "AUG_TomCat"
	if item_texture:
	
		item_texture.texture = load("res://Sprites/Prisma 2/" + item_name + ".png")
		
		var stack_size = int(JsonData.item_data[item_name]["StackSize"])
		item_quantity = randi() % stack_size + 1
		if stack_size == 1:
			$Label.visible = false
		else:
			$Label.text = String(item_quantity)
		
	else:
		
		print("TextureRect node does not exist!")

func set_item(nm, qt):
	item_name = nm
	item_quantity = qt
	if item_texture:
	# 
		item_texture.texture = load("res://Sprites/Prisma 2/" + item_name + ".png")
	
		var stack_size = int(JsonData.item_data[item_name]["StackSize"])
		if stack_size == 1:
			$Label.visible = false
		else:
			$Label.visible = true
			$Label.text = String(item_quantity)
	else:
		
		print("TextureRect node does not exist!")


func add_item_quantity(amount_to_add):

	item_quantity += amount_to_add
	$Label.text = String(item_quantity)
	
func decrease_item_quantity(amount_to_remove):
	item_quantity -= amount_to_remove
	$Label.text = String(item_quantity)

Whenever I open the inventory it prints “TextureRect node does not exist!” twice.
This is what the error looks like in the debugger:
E 0:00:01.666 get_node: (Node not found: “TextureRect” (relative to “/root/Inventory/GridContainer/Slot1/@@2”).)
<C++ Error> Method failed. Returning: nullptr
<C++ Source> scene/main/node.cpp:1476 @ get_node()
Item.gd:3 @ _ready()
Slot.gd:33 @ initialise_item()
Inventory.gd:16 @ initialize_inventory()
Inventory.gd:10 @ _ready()

I will appreciate any help that you guys can give me :slight_smile:
Capture1

Please use the </> tool to properly format your code. Also include an image or a text version of your tree so we can debug. Help us to help you!

1 Like

Sorry i have now edited the post :slight_smile:

1 Like

You are probably calling set_item() before the node is ready so it won’t get its children and they will be null. Try adding:

func set_item(nm, qt) -> void:
	if not is_inside_tree():
		# wait until the node is ready if not inside the tree
		yield(self, "ready")
		
	# Your code here
1 Like

The path you are given in the error message is not the path you show in your image nor is it the path you stated.
Is it possible you have more than one error condition going on; one is the error in the message and the second is the printed message you set up in the _ready() function?

Is that the item script you are showing?

I went to look at the tutorial files found here and it seems like the error message is coming from the Inventory Scene which contains the GridContainer and all the Slots.
So that error is happening because it trying to build a path from the Inventory scene down to a TextureRect which doesn’t exist in the Inventory scene.

thank you for this response, it seems to have removed the issue with the set_item(), but I’m still getting the issue with the ready function not detecting the nodes. do you know a possible solution for this?

Are you creating a new instance of the Item script and not the scene? That error is happening because a node with this script just entered the tree but it has no child node called “TextureRect” so you might be creating a new node with the script or using a different scene that also uses the same script