I am trying to convert my inventory system from unity to Godot and when I try and grab an item in the inventory it gives me this error, I'm unsure why this is or how to fix it. I'm aware what the error means but I don't understand why its giving me it as it's a reference script not an Object? Any help would be appreciated
Inventory.cs
[Serializable]
public class ItemSlot : Resource
{
public ItemResource item;
public int count;
public void Copy(ItemSlot slot)
{
this.item = slot.item;
this.count = slot.count;
}
public void Set(ItemResource item, int count)
{
this.item = item;
this.count = count;
}
public void Clear()
{
this.item = null;
this.count = 0;
}
}
public class Inventory : Resource
{
[Export] public List<ItemSlot> inventoryItems = new List<ItemSlot>();
GameManager.cs
public class GameManager : Node
{
public static GameManager instance;
internal Inventory inventory;
public override void _Ready()
{
inventory = ResourceLoader.Load<Inventory>("res://Player/Inventory.tres");
}
Toolbar.cs
public class Toolbar : Node2D
{
public ItemResource GetItem
{
get
{
//Error occurs here:
return GameManager.instance.inventory.inventoryItems[selectedTool].item;
}
}
}
Error Output:
E 0:00:00.790 ItemResource Toolbar.getGetItem(): System.NullReferenceException: Object reference not set to an instance of an object.
<C++ Error> Unhandled exception
<C++ Source> C:\Users\user\Documents\Godot\Projects\Assets\Scripts\Player\Toolbar.cs:28 @ ItemResource Toolbar.getGetItem()()
Toolbar.cs:28 @ ItemResource Toolbar.getGetItem()()
Toolbar.cs:80 @ void Toolbar.Equip()()
Toolbar.cs:19 @ void Toolbar.Ready()()