string on GUI.Button not showned

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.

Why 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).

Hm... no. It does appear to be inside the loop. Not?

I 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.

@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".

2 Answers

2

You might have a counter to determine how many buttons you have already placed in a vertical or horizontal layout. Here’s an example of a way to layout a series of columns of buttons that break each column after a certain number.

		int itemCount = 0;
		GUILayout.BeginHorizontal();

		GUILayout.FlexibleSpace();

		GUILayout.BeginVertical();

		foreach(ItemType item in inventoryItems)
		{
			
			if(GUILayout.Button(item.name))
			{
				// etc
			}

			GUILayout.Space(20);

			itemCount++;

			if(2 == itemCount)
			{
				itemCount = 0;
				GUILayout.EndVertical();
				GUILayout.BeginVertical();
			}

		}
		GUILayout.EndVertical();

		GUILayout.FlexibleSpace();

		GUILayout.EndHorizontal();

I like this! I defenitly need to learn Unitys GUI framework better. :)

I solved it. It was nothing to do with this code.
InventoryItems was a list but was poulated with SpaceWood that derives from InventoryItems. This made the data show up while debug stepping but not showing in the game.

Sorry for taking your time!!!

+1 for posting solution.

oh right, I see what you mean. If I'm understanding what you're doing, for that I might do something like.. "(item as SpaceWood).name" ...to get specific members of derived classes in a context where they polymorph to a more generic form. This kinda has a bad code smell to do this, though, so its really just to get you by as a quick fix or hack.