Array index out of range. But it doesn't appear to be.
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[i].count > 0)
{
//Debug.Log("a");
slots[i].color = new Color(1, 1, 1, 1);
slots[i].sprite = items[character.items[i].id];
slots[i].transform.GetChild(0).GetComponent<Text>().text = character.items[i].count.ToString();
}
else
{
slots[i].sprite = null;
slots[i].transform.GetChild(0).GetComponent<Text>().text = null;
slots[i].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.
"I filled 63 items with sprites and 36 slots"
I guess that is a typo and both arrays have the same length? Please also tell us what line is causing the error. I suspect this one:
items[character.items[i].id];
Did you check the value of "id"?
Edit: Just saw you have three arrays, so: Did you check that character.items.Length >= slots.Length and all ids from the items are < items.Length?
character.items and slots have the save length, ids from character.items range from 1 to 18 and items.length is 63
Well, if that is the line, you can simply printout the values to see exactly what is wrong there.
Put this line right before the line with the error:
Debug.Log("slots.Length: " + slots.Length + "\nitems.Length: " + items.Length + "\ncharacter.items.Length: " + character.items.Length + "\ni: " + i + "\ncharacter.items[i].id: " + (character.items.Length > i ? character.items[i].id : "undefinded"));
Also, I've set the array length and their content in the editor, so @$$anonymous$$oSR your " = new ..." lines will erase the sprites and images, that I assigned.
Answer by ArturoSR · Aug 21, 2016 at 10:40 PM
Hi, ok, I check your code and seems everything it is ok, but maybe you forgot a little detail:
public Sprite[] items = new Sprite[0];
public Image[] slots = new Image[0];
//In some part of the code
items = new Sprites[5]; // this creates a new array with five elements.
images = new Image[10]; //Same as before.
if (items.Length < images.Length) { //Of course it is. ;-)
//Do Something here. . .
}
If you don't do this, you will get the Debug Error: out of range, count 1 - existing 0.
This will erase editor's assignments, maybe I should load stuff from the script, not using the editor?
I did try this, the errors went away, but now everything I've set up in the inspector is gone.
Answer by mcmikecreations · Aug 22, 2016 at 10:19 PM
Okay, thanks everyone, who answered, it DID help me. The problem was: IDK why, but unity keeps screwing up it's editor assignments. So, I decided to assign every sprite and image in script, using @ArturoSR answer. Thanks to you all.
Hey thanks for your comments, what I showed before was only an example that how is suppose to manage an array of objects, no matter the type, sorry if a make you troubled, but I don't have to much free time yesterday to write new comments, but a see that you could manage the problem very well, maybe it is a little to late but, how suppose you got the items and images in first place?, because, if you let me say this, I works a lot with this type of cases and I never get my component screwed up by the editor, I recommend to you to think about the inspector more like a block of pieces, one at a time, this way could be more easy to you, any doubts you can contact me any time, cheers.