I get an error when I add the script to auto upload.

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

Hello everyone,

I have a problem that I cannot solve. I added the script I defined on my stage from the automatic loading section of the project settings, but I cannot update the tag added to my stage.
There’s only this in the script:

extends Spatial
func _ready():
	$Label.text = "hello godot"

the error he gave:
Invalid set index ‘text’ (on base: ‘null instance’) with value of type ‘String’.

:bust_in_silhouette: Reply From: Wakatta

Most likely because the NodePath of $Label has changed in reference to the script

Your setup may have been like this:

root (Viewport)
 ┖╴Stage (Spatial) (Script.gd)
    ┖╴Label (Label)

and is now like this

root (Viewport)
 ┠╴Spatial (Spatial) (Script.gd)
 ┖╴Stage (Spatial) 
    ┖╴Label (Label)

Which presents two problems.

  1. The location of $Label is now at a different NodePath in reference to the script
  2. The Script gets loaded before the $Label node enters the scene tree and cannot be referenced

Solution
Create a scene where the $Label node is a child of the scripted node and add that scene as the autoload singleton instead of just the script

or

Use an onready variable to the new NodePath of the $Label node

extends Spatial
onready var label = get_node("/root/Stage/Label")

func _ready():

    $label.text = "hello godot"