Can't access variable from another script

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

I tried to import a variable from a script to another but for some reason it’s giving me an error, saying that the script is null

here’s the code from the script i’m trying to access the variable:

onready var GridSizeUI_Script = load("res://Scripts/Grid_Size_UI.gd").new()

#Size of grid in a 1x1 basis ( User Input )
var GridSize = GridSizeUI_Script.NT

here’s the variable from the other script:

var NT = 1

and here’s the error:

Invalid get index 'NT' (on base: 'Nil').
:bust_in_silhouette: Reply From: Inces

First variable is ONREADY so it comes later than second variable.

Try to make GridSize onready also. However it still may be too slow, because you are both loading and creating instance of new script.

Removing the Onready on the first var worked! Thanks a lot!

I didn’t understand what an “Onready” var meant exactly so thanks for the explanation too!

Megatt4 | 2021-10-06 00:37

onready var is the same as you would introduce value of a viariable in on_ready() function :slight_smile:
It is mostly usefull for variables keeping references to children or parent nodes, because when referenced node is not ready at the moment od variable introduction - it will return null for the rest of running project. “Onready” keyword waits for the referenced node to be ready, so the variable can return this node.

I am glad I helped :slight_smile:

Inces | 2021-10-06 07:48

:bust_in_silhouette: Reply From: SF123

You could try setting the script as an Autoload Singleton in project settings.

Other than that, you could try updating to the latest version.

À không liên quan lắm nhưng dịch này buồn quá mọi người ạ hic

Batdongsanbacgiang | 2021-10-09 21:18