0 votes

Hello,
I'm trying to make a simple topdown 2d game in Godot and I've stumbled upon a problem I can't seem to fix.
I have a Label that i want to show when to player goes near the apple but when I try to run the code the moment the player is in the area of the apple I get this error:

Invalid set index 'visible' (on base 'null instance') with value of type 'bool'

I've searched online for a solution and tried different ones but with no success.
What should I do to fix this error?
Any help is appreciated, thank you.
I have attached some screenshots below.
enter image description here

enter image description here

enter image description here

enter image description here

Godot version 3.4.4.stable
in Engine by (14 points)

2 Answers

0 votes

I think it is because you've made the UI script an 'autoload' script.

Autoload scripts are loaded before anything else in the scene tree has loaded. So, when you write

onready var LL = ...

it means that the engine will try to assign the Label node to the LL variable as soon as UI has been loaded (which is before any other nodes). Result : the engine cannot find the Label node, so it assigns 'null' to the variable.

If you really want the LL variable to exist inside the UI script, you could let the Label node register itself. The UI script has :

var LL ( ! not 'onready')

And the Label script class has :

func _onready():
   UI.LL = self

The _onready() function fires as soon as the Label node enters the scene tree.
UI, which is an autoload script, is available from anywhere in your project ( except from other autoload scripts which are loaded before it ).

by (101 points)
+1 vote

Remove UI.gd from AutoLoad and add UI.tscn instead.

by (2,298 points)
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 Frequently asked questions and 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 [email protected] with your username.