So, I have these variables:
public Sprite[] items;
public Image[] slots;
In editor, I filled 63 items with sprites and 36 slots with UI images /w text & image as children from scene.
Then I have this function:
void LoadItems(int count)
{
for (int i = 0; i < count; i++)
{
if (character.items*.count > 0)*
{
//Debug.Log(“a”);
slots*.color = new Color(1, 1, 1, 1);*
slots_.sprite = items[character.items*.id];
slots.transform.GetChild(0).GetComponent().text = character.items.count.ToString();
}
else
{_
_slots.sprite = null;_
_slots.transform.GetChild(0).GetComponent().text = null;_
_slots.color = new Color(1, 1, 1, 0);
}
}
}
Which is called from 2 places which call errors:
if (Time.frameCount == 1 && character != null)
{
LoadItems(slots.Length);
}
//---------------
if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
{
Debug.Log(“ai”);
LoadItems(9);
}*_
Error: IndexOutOfRangeException: Array index is out of range.
I really don’t know, what is wrong. Maybe it’s the editor?
Oh and character.items is this:
public Item[] items;
//This is Image script
[System.Serializable]
public class Item {
public int id;
public byte count;
public string info;
public Item(int id, byte count, string info)
{
this.id = id;
this.count = count;
this.info = info;
}
}
Edit: items[] is just an array of sprites, used to represent textures. I have 63 in-game textures, so I have 63 Sprites, 1 for each item. Slots on the other hand are actual on-screen inventory slots, where the items are stored. And character.items is basically the same array as slots, it holds data of each slot, what the item is (id, also used in Sprite[] items), what is it’s count, and additional info.