Godot Version
4.3.stable.mono
Question
I am trying to make a UI to display resources needed to construct a building. I have a UI script that gets an array of InventoryItems from the building being constructed. InventoryItem is a array, with each element consisting of an ItemDataResource and an amount – the ItemDataResource contains the ItemName, ButtonIconName, etc. for the item
I request the array from the building object, and it comes back OK. I can GD.Print the length of the returned array and GD.Print the ButtonIconName on element[0] of the array, and it works fine, with no error
The problem comes when I try to iterate through the array in a loop. I get a null reference error, even though the GD.Print statement still works
public void UpdateConstructionUI(BuildingConstruction bldg)
{
InventoryItem[] itemList = bldg.GetConstructionList();
GD.Print("ConstructionUI: itemList.Length = " + itemList.Length);
GD.Print("ConstructionUI: " + itemList[0].itemResource.ButtonIconName);
for (int i = 0; i < itemList.Length; i++)
{
GD.Print($"ConstructionUI: i = {i}, ButtonIconName = {itemList[i].itemResource.ButtonIconName}");
}
}
This produces the output:
ConstructionUI: itemList.Length = 2
ConstructionUI: Block.png
i = 0, ButtonIconName = Block.png
Even though the code works and I get the desired output, I get a null reference on the last line and the program dies without completing the loop
Why can I do this using 0 for the index, but when I use i (with a value of 0) it fails?
What am I missing here?