+1 vote

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').
Godot version Godot 3.2.3
in Engine by (34 points)

2 Answers

+2 votes
Best answer

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.

by (3,378 points)
selected by

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!

onready var is the same as you would introduce value of a viariable in on_ready() function :)
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 :)

+1 vote

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

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

by (89 points)

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.