How to fix null instance error when changing a label's text

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

Whenever I try to update my question label for the quiz I am creating, I keep on getting the error Invalid set index “text” on base null instance with value of type “String”, originally I was not getting this error, but as soon as I made the script global I started to get this error, so I don’t know if that is what’s affecting my program, the code is below

$Question.text = questions[current_question][0]
$Answer1.text = questions[current_question][1]
$Answer2.text = questions[current_question][2]
$Answer3.text = questions[current_question][3]
$Answer4.text = questions[current_question][4]

as for the nodes I have a control node called quiz as the root node, the questions and answers are all direct children of quiz and the script is attached to the quiz node, would appreciate any help

:bust_in_silhouette: Reply From: kidscancode

“null instance” is what you get when you give get_node() an invalid path. $Question is no longer a valid path.

Your problem comes from this: “I made the script global”. When you set a script as a global, Godot creates a blank node to add it to. That node has no children, therefore all your node references are not valid.

What you should do instead is make the scene, with the question and answer nodes, and the script attached, the global instead.

It worked! Thanks so much

sylves1x | 2022-12-19 03:47