Topic was automatically imported from the old Question2Answer platform.
Asked By
GM12
Hello Godoters,
I have been creating a text game by following the tutorials on youtube(here is the link https://www.youtube.com/watch?v=5gP1eOtR5Kg&t=1205s ) and I came to the point where I have to save the reusable scene by adding it to the GDScript here is the code:
extends Control
onready var history_rows = $Background/MarginContainer/Rows/GameInfo/HistoryRows
func _on_Input_text_entered(new_text: String) → void:
var input_response = InputResponse.instance()
history_rows.add_child(input_response)
The problem is whenever I launch the game,the debugger pops up with the error message: attempt to call function ‘add_child ’ in base ‘null instance ’ on a null instance.
A node doesn’t exist at the node path Background/MarginContainer/Rows/GameInfo/HistoryRows at the time your control node that the script is attached to is _ready().
First try make sure that the NodePath listed above is valid.
If the NodePath above is valid and you are still getting the error, move the node that the script is attached to up in the scene tree so its _ready() function gets called later. Alternatively change your function, so you know that all nodes will be in the scenetree when you attempt to get them:
func _on_Input_text_entered(newtext: String) -> void:
if not history_rows:
history_rows = get_node("Background/MarginContainer/Rows/GameInfo/HistoryRows")
var input_response = InputResponse.instance()
history_rows.add_child(input_response)