I created a health component and that health component has an export var for a health bar scene that I created. I drag the progress bar into the export slot in the inspector. I set the progress bar max value in the _ready() function of the health component but when I run the game it says that I can’t set max value of Nil. Am I missing something that I need to do to make this work? I am not sure what else I need to do other than drag the node to the export slot.
In the image below the script is the one that is attached to the HealthComponent. You can see the export var for the progress bar and I dragged the node in the scene tree to the export row in the inspector. You can also see the max_health set in _ready(), that is where it’s failing
Just a wild guess here, I think this has to do with the order Godot loads scenes in the scene tree from top to down. It might be that healthbar has not been loaded when the healthcomponent is looking for it.
Try adding healthbar as a child of healthcomponent.
.new() creates a new object of the type of the script.
So you created a new HealthComponent without a definition for the health_bar variable export.
The PackedScene is the node complete with the attached script and defined health_bar export variable.
I had the same problem. I was going to ask here, but then suddenly solved it.
My problem was that I had an @export array of cameras that I added in the inspector, that worked splendidly for days. Until suddenly without changing any related code, the exported array became to be always recognized as Nil and nothing worked to fix it.
What solved it for me is that I moved that @export array to just above _ready(). I have no idea why it solved it. Also I have no idea why it broke in the first place as I did no changes to the code.
So just sharing what (for whatever reason) fixed it for me.
(Moving it just above _ready() might not even be the fix by itself, maybe just moving around things somehow ‘unclogged’ something in the interiors of Godot. I don’t know…)
UPDATE: So, after writing this comment, this started happening again for many other scenes as well… And what I noticed is that, sometimes changing the @exports in the scene file itself carries over to your already instanced scenes. And sometimes they don’t. So I managed to fix the problems by changing the instanced scenes’ exported vars to be the same as in the base scene. (The confusing part is that sometimes when you change things in the base scene, they do carry over to already instanced scenes, and sometimes not. So if this issue with @exports arises, I suppose it’s good to also check the instanced scenes vs. base scene and whether they differ. At least this was my experience.)
Thank you! Same problem, I had @export an array of meshes wich I wanted to change the material, and it didnt carry over to the instantiated scene, very weird!
I faced this issue as well and what worked was moving the code from the _init() to the _enter_tree() function
The actual issue, as others here mentioned, is that for some reason the reference to the exported variable was called while the variable itself was null.
The reason I’m saying this is because calling any function on the variable in, say, the process function worked perfectly well, while calling them in the _init function gave the error.