I am having a Null error that I cannot figure out how to solve, unlike my other Null errors. The error in the Unity console states
"NullReferenceException: Object reference not set to an instance of an object
Inventory.ClearSelectedItemWindow () (at Assets/Scripts/Player/Inventory.cs:202)
Inventory.Start () (at Assets/Scripts/Player/Inventory.cs:62)"
with another error under it reading
"NullReferenceException while executing 'started' callbacks of 'Main/Inventory[/Keyboard/q]'
UnityEngine.InputSystem.LowLevel.NativeInputRuntime/<>c__DisplayClass7_0:<set_onupdate>b__0 (UnityEngineInternal.Input.NativeInputUpdateType,UnityEngineInternal.Input.NativeInputEventBuffer*)
UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate (UnityEngineInternal.Input.NativeInputUpdateType,intptr)"
The console says the error is on line 202 in my Inventory script.
void ClearSelectedItemWindow ()
{
selectedItem = null;
selectedItemName.text = string.Empty;
selectedItemDescription.text = string.Empty;
selectedItemStatNames.text = string.Empty;
selectedItemStatValues.text = string.Empty;
useButton.SetActive(false);
equipButton.SetActive(false);
unEquipButton.SetActive(false);
dropButton.SetActive(false);
}
which is connected to the full script here
public void SelectItem (int index)
{
if(slots[index].item == null)
return;
selectedItem = slots[index];
selectedItemIndex = index;
selectedItemName.text = selectedItem.item.displayName;
selectedItemDescription.text = selectedItem.item.description;
selectedItemStatNames.text = string.Empty;
selectedItemStatValues.text = string.Empty;
for(int x = 0; x < selectedItem.item.consumables.Length; x++)
{
selectedItemStatNames.text += selectedItem.item.consumables[x].type.ToString() + "\n";
selectedItemStatValues.text += selectedItem.item.consumables[x].value.ToString() + "\n";
}
useButton.SetActive(selectedItem.item.type == ItemType.Consumable);
equipButton.SetActive(selectedItem.item.type == ItemType.Equipable && !uiSlots[index].equipped);
unEquipButton.SetActive(selectedItem.item.type == ItemType.Equipable && uiSlots[index].equipped);
dropButton.SetActive(true);
}
void ClearSelectedItemWindow ()
{
selectedItem = null;
selectedItemName.text = string.Empty;
selectedItemDescription.text = string.Empty;
selectedItemStatNames.text = string.Empty;
selectedItemStatValues.text = string.Empty;
useButton.SetActive(false);
equipButton.SetActive(false);
unEquipButton.SetActive(false);
dropButton.SetActive(false);
}
public void OnUseButton ()
{
if(selectedItem.item.type == ItemType.Consumable)
{
for(int x = 0; x < selectedItem.item.consumables.Length; x++)
{
switch(selectedItem.item.consumables[x].type)
{
case ConsumableType.Health: needs.Heal(selectedItem.item.consumables[x].value); break;
case ConsumableType.Hunger: needs.Eat(selectedItem.item.consumables[x].value); break;
case ConsumableType.Thirst: needs.Drink(selectedItem.item.consumables[x].value); break;
case ConsumableType.Sleep: needs.Sleep(selectedItem.item.consumables[x].value); break;
}
}
}
If you have any idea why the error is happening or need more of my inventory script please get back to me.
What I have tried:
I have tried every other common method I use to fix the Null errors but none of them are working or helping me out this time.