- Home /
Referencing an array but its returning a nullrefrence exception?
So this one is really strange... I am attempting to create an armor system that shows up visually, and to do so I have this script that stores what armor is equipped (In an array public currentEquipment[])and then sets the images to the corresponding icons of the items in the array. (The objects in the array are scriptable "equipment" objects. And while this code lets the game continue running, it spits out a nullrefrence exeption every frame on all the armor images in the UI that dont have a armor object assigned to them. (The 'equipment' items all have a sprite associated with them that can be referenced other places just fine.) I am very very confused and I have no idea why 1.) Im getting these errors, 2.) why the game is still running despite these errors. (Ps. the error is on every line of void Update() where currentEquipment[] is called.)
Here is the code:
#region singleton
public static EquipmentManager instance;
void Awake()
{
instance = this;
}
#endregion
public Equipment[] currentEquipment;
public Image Head;
public Image Body;
public Image Legs;
public Image Charm;
Inventory inventory;
void Start()
{
inventory = Inventory.instance;
int numSlots = System.Enum.GetNames(typeof(EquipmentSlot)).Length;
currentEquipment = new Equipment[numSlots];
Head.enabled = false;
Body.enabled = false;
Legs.enabled = false;
Charm.enabled = false;
}
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;
}
void Update()
{
if (currentEquipment[0].icon != null)
{
Head.enabled = true;
Head.sprite = currentEquipment[0].icon;
}
else
{
Head.sprite = null;
Head.enabled = false;
}
if (currentEquipment[1].icon != null)
{
Body.enabled = true;
Body.sprite = currentEquipment[1].icon;
}
else
{
Body.sprite = null;
Body.enabled = false;
}
if (currentEquipment[2].icon != null)
{
Legs.enabled = true;
Legs.sprite = currentEquipment[2].icon;
}
else
{
Legs.sprite = null;
Legs.enabled = false;
}
if (currentEquipment[4].icon != null)
{
Charm.enabled = true;
Charm.sprite = currentEquipment[4].icon;
}
else
{
Charm.sprite = null;
Charm.enabled = false;
}
}
}
Answer by s_awali · Nov 26, 2018 at 10:09 AM
No wonder your code throw a couple of NullPointerException, let me explain why:
Your Equip() function is never called in your code, so your currentEquipment is always empty.
Even if you add equipment in this array via the inspector, it is wiped out on startup, because you call currentEquipment = new Equipment[numSlots]; in OnStart() method.
You do not do enough controls in your tests:
if (currentEquipment[0].icon != null)
should beif (currentEquipment[0] != null && currentEquipment[0].icon != null)
to make sure there is an equipment in that specific array index.
Waiting for your feedback :)
NB: Oh and by the way, only your script crash, not the whole game, that's why it still run!
Thanks, that fixed it. I guess I thought it was unusual for so many errors to not be effecting the game.
Answer by ahstein · Nov 26, 2018 at 06:45 AM
I think you probably need to check that currentEquipment[0] != null before you check that currentEquipment[0].icon != null.
In response to #2: Errors don't necessarily make your game crash. There's a setting that makes the game pause when you get an error--you may have disabled it.
Thanks a ton, this was correct but I can only accept one answer and I just happened to read the second one first.
Your answer
Follow this Question
Related Questions
Specify from inspector which scriptable field to use 0 Answers
How to Find the Sum of each Individual item / array out of range 0 Answers
Can't open dialogue box when pressing button 0 Answers
How can I instantiate parented ui objects above previously instantiated children? 1 Answer
GUI On button click change visibility of array objects 0 Answers