A script created node can't find other nodes.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Famos

I have a fight scene with the hierarchy of

Main
-Player
-UI

UI creates another node, Enemy, and connects to it using

var root = get_tree().get_root()
root.add_child(enemy_node)
connect("enemyTurn", enemy_node, "_on_UI_enemy_Turn")

This works fine, now in the Enemy node, I try to find the UI node with

var root = get_tree().get_root()
var ui_node = root.find_node("UI")

Printing root and ui_node gives

root:[Viewport:1220][Object:null]
:bust_in_silhouette: Reply From: Shiva

Try using get-node() instead of find-node().

:bust_in_silhouette: Reply From: ponponyaya

The method find_node has an bool parameter “owned” which has default variable true.

You can take a look at Godot Doc, it says that “If owned is true, this method only finds nodes whose owner is this node. This is especially important for scenes instantiated through a script, because those scenes don’t have an owner.”

In your case, root = get_tree().get_root() don’t have owner, so you get “Object : null”.