Help with get_node in multiple scripts returning null

Godot Version



I am building a simple clicker card game. Currently I am able to get certain things to work by setting everything in an autoload singleton class. My issue is when I start trying to get_node in other scripts that are set on the root nodes of different scenes outside of the main game scene. I am using the “instantiate child scene” button to add the other scenes into the main game as I have seen in many tutorials. Currently the issue I am running into is when I am trying to get the node in the UI scene that I am running.

In the following code, during the ready function, it cannot set the text because it is returning a null value. I was running into the same issue in another location and just added a path with the get_node all the way back to the root node.

class_name CardUI
extends CanvasLayer

@onready var unlock_card_space_button: Button = get_node("UnlockCardSpace")

func _ready() -> void:
	unlock_card_space_button.text = "Card\nSpace\n0/4\nCost: $100"

func unlock_card_space() -> void:
	print("made it")

Is there something I am missing when trying to run the get_node? I have tried multiple different combinations of the get_parent, get_child, and get_node. All ending up returning null. Like I said before I can run it out of a autoload class but as soon as I add the same code with the path to the root it returns null.

When I view the remote while the game is in a breakpoint I see the autoloads that I am doing are siblings of the main game scene. Is there a different way to point to nodes because of that issue?

Fair warning I am still new to Godot and learning through tutorials and reading the documentation. I know its all part of the learning process just need some help bouncing ideas. Thanks! :smiley:

Remote Tree

Update to this. I am able to update the text to the button during the _ready function. But it errors out when running the unlock_card_space when trying to update the text.

class_name CardUI
extends CanvasLayer

@onready var unlock_card_space_button: Button = $/root/Game/UI/UnlockCardSpace
var card_space_unlocks = 0
var card_space_cost = 100

func _ready() -> void:
	unlock_card_space_button.text = "Card\nSpace\n0/4\nCost: $100"

func unlock_card_space() -> void:
	if card_space_unlocks < 4 and Deck.total_money > card_space_cost:
		Deck.total_money = Deck.total_money - card_space_cost
		card_space_unlocks += 1
		card_space_cost = card_space_cost * 1.10
		unlock_card_space_button.text = "Card\nSpace" + str(card_space_unlocks) + "\n/4\nCost: $" + str(card_space_cost)

I believe the error at this point is how the function unlock_card_space() is being called. I have a UI scene that has the Button pressed signal being used the call the unlock card space. Is there something inside the CardUI script I need to add to make sure that signal is being processed correctly?

For future reference: get_node() expects a node path, not just the name of the node. You can use find_child() if you want to find a node by it’s name.
You can also drag&drop the node into the code while holding CTRL to create a reference variable to it.

What’s the error message you’re getting in unlock_card_space()?

The error I am getting now is:

Invalid set index ‘text’ (on base: ‘Nil’) with value of type ‘String’.

I am pretty sure it has something to do with how the function itself is being called at this point. I can create a break in the code and it find the Button node to update the text with the _ready function. But once the button is pressed it gets set to null so it is no longer able to see the Button node.

Yeah that shouldn’t happen based on this code you shared. If it works in _ready() then it should work after that too.
The variable should keep the reference to the button, unless you have something like unlock_card_space_button = "something_else" somewhere in your code, or you delete the button node itself.

You’re right. Everything is working… I just had the code in the wrong UI script. I have a card UI and game UI. just overlooked it. Have to be better about my naming apparently. Thanks for the help!

1 Like