So, I was trying to program a equipment item for an rpg, but when I try to call a method I get a NullReferenceException, can anyone help me to solve this?
Equipment Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu (fileName = "New Equipment", menuName = "Inventory/Equipment")]
public class Equipment : Item
{
public int armorMod;
public int dmgMod;
public EquipmentSlot equipmentSlot;
public override void Use ()
{
base.Use ();
EquipmentManager.instance.Equip (this); // Error happens here.
RemoveFromInventory ();
}
}
public enum EquipmentSlot {Head, Chest, Legs, Feet, rightHand, leftHand}
Equipment Manager Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EquipmentManager : MonoBehaviour
{
#region Singleton
public static EquipmentManager instance;
void Awake ()
{
instance = this;
}
#endregion
Equipment[] currentEquip = new Equipment[6];
Inventory inventory;
public void Equip (Equipment newItem)
{
int slotIndex = (int)newItem.equipmentSlot;
Equipment oldEquip;
if (currentEquip [slotIndex] != null)
{
oldEquip = currentEquip [slotIndex];
Inventory.instance.Add (oldEquip);
}
currentEquip [slotIndex] = newItem;
Inventory.instance.AddEquipment (newItem);
}
}
Edit:
So, I managed to make it work, there was a lot wrong with the code, and not only with the classes I posted, now I have a functioning equipment inventory (I’m so proud), thanks ![]()