- Home /
Trying to assign UI images from an array with scriptable objects not working?
So I am making an armor system, and so when the player equips a new peice of armor, it goes into the public armor array that can be acessed anywhere. In order to show it visually, I made a script that should always be setting each respective 'box' in your inventory to that armor peice to show you have equiped it... but of course Im getting an error that says object refrence not set to an instance of an object. Im not sure what isn't defined here.. the aray is full of scriptable 'items' that have an icon, and I know that it can work because without all the if else statements, I tried setting it manually and the image changed correctly, but it still gave me an error despite working fine. I am very confused. Any ideas?
Here is the script that manages the UI of the armor:
public Image Head;
public Image Body;
public Image Legs;
public Image Charm;
//public Image Weapon;
public Image tets;
EquipmentManager equipManager;
void Start()
{
equipManager = EquipmentManager.instance;
}
void Update()
{
if (equipManager.currentEquipment[0].icon != null)
{
Head.enabled = true;
Head.sprite = equipManager.currentEquipment[0].icon;
}
else
{
Head.enabled = false;
}
if (equipManager.currentEquipment[1].icon != null)
{
Body.enabled = true;
Body.sprite = equipManager.currentEquipment[1].icon;
}
else
{
Body.enabled = false;
}
if (equipManager.currentEquipment[2].icon != null)
{
Legs.enabled = true;
Legs.sprite = equipManager.currentEquipment[2].icon;
}
else
{
Legs.enabled = false;
}
if (equipManager.currentEquipment[4].icon != null)
{
Charm.enabled = true;
Charm.sprite = equipManager.currentEquipment[4].icon;
}
else
{
Charm.enabled = false;
}
}
Here is the script with the equipment array in it:
#region singleton
public static EquipmentManager instance;
void Awake()
{
instance = this;
}
#endregion
public Equipment[] currentEquipment;
Inventory inventory;
void Start()
{
inventory = Inventory.instance;
int numSlots = System.Enum.GetNames(typeof(EquipmentSlot)).Length;
currentEquipment = new Equipment[numSlots];
}
public void Equip(Equipment newItem)
{
int slotIndex = (int)newItem.equipSlot;
Equipment oldItem = null;
if(currentEquipment[slotIndex] != null)
{
oldItem = currentEquipment[slotIndex];
inventory.Add(oldItem);
}
currentEquipment[slotIndex] = newItem;
}
}
The Exact Error is:
NullRefrenceExeption: Object reference not set to an instance of an object EquipmentUI.Update() (At Assets/EquipmentUI.cs 26)
Can you paste the line the error is referring to? If it's this one: Head.enabled = false;
then Head was not set before that line. Did you actually set it in the inspector? Silly questions, I know. Also could you paste the contents of Equipment class in case?
This will still fail, but it as long as you don't intend to make big changes per different parts, might be a bit cleaner:
for (int i=0; i<equip$$anonymous$$anager.currentEquipment.Length; i++) {
Image part;
switch(i) {
case 0:
part=Head;
break;
case 1:
part=Body;
break;
case 2:
part=Legs;
break;
case 4:
part=Charm;
break;
default:
continue;
break;
}
if (equip$$anonymous$$anager.currentEquipment[i].icon != null)
{
part.enabled = true;
part.sprite = equip$$anonymous$$anager.currentEquipment[i].icon;
}
else
part.enabled = false;
}
Also, it might be more efficient to change these from the Equipment$$anonymous$$anager than checking constantly in this Update().
This still threw the same error, so I am starting to think it may be something to do with the equipment array itself. Im going to put that code on the question.
Answer by Matthew_Ostil · Nov 21, 2018 at 06:55 PM
Try moving your caching of the EquipmentManager into the Awake method:
void Awake()
{
equipManager = EquipmentManager.instance;
}