How to solve godot cant find node

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

enter image description here

Whenever i tried to run the game, it said attempt to call function ’ add child’ in base null instance on a null instance. The GridContainer node is already directly under the Calibration1 root node but somehow it doesn’t detect it?

Why are you storing the node the script is on in a variable?

exuin | 2021-04-27 19:33

what do you mean

slyyy | 2021-04-27 19:42

You have

onready var Game = get_node("/root/Calibration1")

Isn’t Calibration1 the node that the script is on?

exuin | 2021-04-27 19:46

cardcalibration is the script for calibration1 control node. this calibration1 script is not attached to any node.

slyyy | 2021-04-27 19:49

So it’s a singleton? Singletons call their ready function before the other nodes so the node probably isn’t ready yet

exuin | 2021-04-27 19:59

so how do i ready the nodes?

slyyy | 2021-04-28 06:46

:bust_in_silhouette: Reply From: exuin

Yield for the ready signal from Calibration1.

yield(get_node("/root/Calibration1"), "ready")

after trying, i find that if i set my main scene as calibration1.tscn, it can run because then the tree of calibration1.tscn will appear in the root (remote).

but when i try to run the other scenes such as main menu, the root changes back to the tree of that particular scene and the the gridcontainer node cannot be found again because then it does not appear in the root (remote)

what way can i solve this? how do i have the tree of this scene always active in the root other than setting it as the main scene? or am i missing something?

slyyy | 2021-04-28 19:41

You must make it an autoload singleton if you always want it to be in the SceneTree.

exuin | 2021-04-28 20:00

yeah, the singleton is there on the scenetree.

but currently the scenetree only have my main menu scene and the singleton active there. my calibration1 tree is not active, therefore the gridcontainer under the calibration1 control node cant be found and therefore throwing me the error.

slyyy | 2021-04-28 20:06

I’m not referring to the Singleton you already have. If you want to have Calibration1 still in the SceneTree even when the main scene is the main menu, you will need to make Calibration1 a Singleton.

exuin | 2021-04-28 20:09

oohh, u mean the scene itself as a singleton.

slyyy | 2021-04-28 20:15

I’m facing another problem whereby if I make calibration1.tscn a singleton, the program can run but the things that suppose to load inside calibration1 is gone.

Right now the things inside calibration1.tscn can only run if I play it from calibration1.tscn but it cant be set a singleton otherwise the things inside wont load apparently.

So basically if I make calibration1.tscn a singleton as you mentioned, the project can run from the main menu but then when it arrives at calibration1.tscn the things inside wont load. If I don’t make calibration1.tscn a singleton, the things inside can load only when played directly from the scene itself since playing the whole project from the main menu would throw the node not find error again.

slyyy | 2021-04-30 14:27