So e (this is not a question of what a index out of range exception it is why this specific one is happening when Im using the same index and method some where else Response to recent mark of repetition)
Essentially I am checking to see if a value in the array is Null. But it keeps giving me an out of range exception when I just used the same principle in another area. This may be a silly question with a silly answer but I've run into the issue and I seem to not be able to solve it.
I have checked that the int going in is correct and that the array is the correct size.
Im sorry for lack of highlights Im new here and am not sure how to apply them.` The Place where it goes Wrong
public void OnDragStop()
{
if (SlotHover.name.Contains("Slot"))
{
string[] OS = PSlot.name.Split('t');
int PSN = int.Parse(OS[1]);
string[] NS = SlotHover.name.Split('t');
SN = int.Parse(NS[1]);
Debug.Log(SN);
if (inventory.Inventoryitems[SN] == null)
{
inventory.Inventoryitems[SN] =
inventory.Inventoryitems[PSN];
inventory.Inventoryitems[PSN] = null;
SlotHover.transform.Find("ItemSlotSprite" +
SN).GetComponent<Image>().sprite =
PSlot.transform.Find("ItemSlotSprite" +
PSN).GetComponent<Image>().sprite;
PSlot.transform.Find("ItemSlotSprite" +
SN).GetComponent<Image>().sprite = EmptySprite;
this.gameObject.transform.position = PP;
}
}
}
The Place where its working
public void AddItem(Item ItemToAdd)
{
for(int i = 0; i < inventory.Inventoryitems.Length; i++)
{
if(inventory.Inventoryitems[i] == null)
{
inventory.Inventoryitems[i] = item;
Sprite sprite = ItemToAdd.DefualtSprite;
Inventory.transform.Find("Slot"+i).transform.Find("ItemSlotSprite" +
i).GetComponent<Image>().sprite = sprite;
return;
}
}
}`
IndexOutOfRange: Array index out of range is what it gives me as shown above Im not sure why in this specific case its giving the exception.