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

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

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.

:bust_in_silhouette: Reply From: siska

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 ).

1 Like
:bust_in_silhouette: Reply From: hilfazer

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