I am sure that inventoryItem.Name contains “Wood” cause i see it when i add a breakpoint. Still i get nothing in the game. Just empty buttons.
void DrawInventoryContent (int id)
{
var buttonIndex = 0;
var x = 0;
var y = 0;
foreach (var item in inventoryItems) {
//create position rectangel for button
var startMargin = buttonMargin;
var leftStartPosition = startMargin + buttonMargin * x + buttonWidth * x;
var topStartPosition = startMargin + buttonMargin * y + buttonHeight * y;
var buttonRectangel = new Rect (leftStartPosition, topStartPosition, buttonWidth, buttonHeight);
//Get inventoryiten by index
var inventoryItem = inventoryItems [buttonIndex];
var name = inventoryItem.Name;
//Create and place buttin
GUI.Button (buttonRectangel, name);
// check if this button is at end of row and change to next row
if (x == buttonsPerRow - 1) {
x = 0;
y++;
} else {
x++;
}
//go to next item
buttonIndex ++;
}
}
I can't tell what's what here. inventoryItem.Name looks like you're calling a static variable from another script, but above that it looks like inventoryItem is an item in an array... The whole script would be helpful.
– clunk47Why are you keeping track of, and incrementing, the inventory items outside of your for loop? Why don't you just use the instance that you're already working on? Your method of storing the buttonIndex seems error prone. //Get inventoryiten by index //Get rid of these two lines //var inventoryItem = inventoryItems [buttonIndex]; //var name = inventoryItem.Name; //Create and place buttin GUI.Button (buttonRectangel, item.Name); clunk47, it appears to be just a property of the 'inventoryItem' class (unknown type).
– iwaldropHm... no. It does appear to be inside the loop. Not?
– Lautaro-ArinoI mean that a reference exists outside of the loop for no obvious reason. You don't use a foreach loop and also manually increment an index counter with it. If you were going to do that then you should use a regular for loop.
– iwaldrop@Lautaro Arino: iwaldrop is just saying that if you're using this... foreach (var item in inventoryItems) { then you need to turn this... var inventoryItem = inventoryItems [buttonIndex]; into this... var inventoryItem = item; Yet, having said that, you don't even need "var inventoryItem" anymore because you can just use "item" directly. So, extrapolating that advice further, you would change all uses of "inventoryItem" afterward to "item", removing "var inventoryItem".
– ThatsAMorais