inventory woes

i have a lot wrong with my inventory, but i’m just going to post one thing i’m having trouble with:

“Array index is out of range”

       // need a system of naming for this method?
        void ShowMerchant1()
        {
            string labeltxt = "";
            string labelimg = "";
            int counter = 0;
            for (int i = 0; i < merchant1_capacity; i++)
            {
                counter = i + 1;
                //update icon and text
                labeltxt = "M1text" + counter;
                labelimg = "M1image" + counter;


                Text text = GameObject.Find(labeltxt).GetComponent("Text") as Text;
                if (merchant1[i].item == null || merchant1[i].item.objname == "Nothing")
                   text.text = "Nothing x 0";
                else
                    text.text = merchant1[i].item.objname + " x " + merchant1[i].quantity;
                Image image = GameObject.Find(labelimg).GetComponent("Image") as Image;
                if (merchant1[i].item == null || merchant1[i].item.objname == "Nothing")
                    image.sprite = Resources.Load<Sprite>("nothingicon");
                else
                {
                    Sprite invicon = Resources.Load<Sprite>(merchant1[i].item.sprite);
                    image.sprite = invicon;
                }
            }
        }

on line 17

i know i’m not supposed to use GameObject.Find but i don’t know what else i can use

my merchant inventory is set to the same as merchant1_capacity in the inspector

i also have trouble with when i open the store by clicking on the merchant - my icons don’t show up (for the player either; unless i have already opened the inventory to view it, then the icons work for the player but still not the store). however, no issue with the text.

Where are you setting merchant1_capacity, and how are you making sure that merchant1_capacity isn’t larger than the merchant1 array?

I’d change merchant1_capacity in the for loop to merchant1.Length and see if that fixes your issue.

1 Like

i don’t know how, but that did fix it. i now have an inkling of what else might be wrong with my merchant and hero inventory exchanges … though not of why my icons don’t show up when i click the merchant. maybe i’ll come across something. enter debugging hell! and, of course, thank you again