JS help updating inventory slot images on buttons

This is what I have so far but I just cant get it to work
It adds all of the UI Button/slot GO’s to an array and then I want it to go through the inventory array search each for component (ITEM.js) and replace the buttons graphic with the one saved on the item

function updateSlots(){
Debug.Log(invArray.length);
Debug.Log(invSlots.childCount);

for( var i : int = 0; i < invArray.length; i++ ) {

for( var ii : int = 0; ii < invSlots.childCount; ii++ ) {
                invSlotsob[ii] = invSlots.GetChild(ii);
            invSlotsob[ii].gameObject.GetComponent(Image).sprite = invArray[ii].gameObject.GetComponent(item).SPR; 
}
}
}

gives me this error

Assets/inventory.js(41,101): BCE0019: ‘gameObject’ is not a member of ‘Object’.

invSlotsob[ii].gameObject.GetComponent(Image).sprite = invArray[ii].gameObject.GetComponent(item).SPR;

what type is the invSlotsod array? did you use “var”'s?

2k, congrats. Also, I’d just like to say that near-meaningless surface errors like this, which obscure the real error happening elsewhere, are reason #76 of why I can’t stand UnityScript. The c# “var” introduction cop-out a few years back nearly made me cry…

Edit: I can’t read this. I guarantee that even if the array is the wrong type, this loop isn’t functioning correctly. At the very least invArray should be accessed with i and not ii, right?

I’m on my mobile at the minute but I’ll try to remember I’m pretty sure I used var invSlotsod = new Array();

Should I change it to a Gameobject array?

The loop works I just can’ figure out how to change the images of the buttons to the ones from the itemsdb based on the Id.
I can do the last part it’s just actually changing the buttons to the right items image.

This is my first time trying to make an inventory and Im only just starting to understand US properly I don’t have a clue where to start with c#

We’ll have to see exactly how you define and initialize it, but if it’s an array of GameObjects then you should create it as a array of GameObjects, yes. “var” means “intuit the type from the context”, which doesn’t work properly unless you initialize it using a specific type. If you initialize it as “array of objects”, that could mean absolutely anything, and it won’t keep track of the kinds of objects it’s carrying. If you’re absolutely certain you have nothing but GameObjects inside, then you can cast the contents to “as GameObject” whenever you reference them, but it’s easier and far safer to just change the type of array to be more specific- you won’t be able to accidentally add other types to the array, and you won’t have to explicitly cast everything when you go to use them.

The reason why I say the loop is wrong is that you’re doing a nested iteration- like looping over “columns” within “rows” to create a grid of boxes for example. That kind of iteration means that the first loop is breaking the problem into “Chunks” (rows), while the second loop is breaking it into “Pieces” (columns within those rows).

In the shared code, the first loop is doing nothing, because the second loop isn’t relying on its iterator (i) at all. The first loop will say “okay, do the following ten times” and the second will do some work the first time, then overwrite that same work with the exact same data times 2-10. Get what I’m saying? At some point during the second loop, the first loop’s iterator has to be referenced in some way, otherwise it’s not doing anything.

There are times where that’s fine (when the process is randomized in some way, then doing the exact same thing 10 times, like shuffling a deck for instance, is fine), but this doesn’t look like one of those.