- Home /
Buttons not showing up the first time the menu loads, but showing up on all menu openings after
I'm trying to program an inventory menu in my game, but for some reason the buttons are not showing up the first time I open it. They are showing up in the hierarchy, but they are not visible. If I close the menu and open it again, they'll be visible every time after the first.
I've checked the invisible buttons in the inspector and everything about them seems identical to the buttons that show up on the second menu opening. Z axis, layer, Image (Script), Button(Script)...
Here's the code I'm using to place the buttons:
public void InitializeInventoryMenu()
{
//Activate inv canvas
InventoryCanvas.gameObject.SetActive(true);
//Clear existing buttons
UI_Inventory_InvButton[] existingButtons = GetComponentsInChildren<UI_Inventory_InvButton>();
foreach (UI_Inventory_InvButton button in existingButtons)
{
button.gameObject.SetActive(false);
}
//Set up buttons
foreach (KeyValuePair<string, StaticData.WoolType> entry in StaticData.staticData.woolTypesDict)
{
if (GameControl.control.woolInventory[entry.Key] > 0)
{
Debug.Log("Adding new entry");
GameObject myObject = Instantiate(Button_Template);
UI_Inventory_InvButton storeButton = myObject.GetComponent<UI_Inventory_InvButton>();
storeButton.SetupButton(entry.Key);
myObject.transform.SetParent(Button_Template.transform.parent);
myObject.SetActive(true);
}
}
}
I have tried changing the order of some of the steps but that hasn't changed the result.
Answer by GreySkyGames · Feb 23, 2019 at 11:01 AM
Moved on to other phases in my project, fiddled with art assets for a bit and came back to it...Turns out I missed something when looking at the values of the "invisible" vs visible buttons. The X, Y, and Z scale were all set to 0 on the invisible buttons. Adding "myObject.gameObject.transform.localScale += new Vector3(1, 1, 1);" fixed the issue.
I really don't understand why the scales got set to zero or why they only got set to zero the first time...if anyone has any insight on that I'm super curious.
Answer by SteenPetersen · Feb 21, 2019 at 08:08 AM
Hi there
First it seems a bit odd that you are running a loop to remove some already existing buttons from the scene when you load the inventory, would it not be easier to have those buttons in a canvas that you can simply disable? I suspect that:
foreach (UI_Inventory_InvButton button in existingButtons)
{
button.gameObject.SetActive(false);
}
Is where your problem is. Perhaps the first time it runs its hides those buttons, and in subsequent iterations it works correctly. When you press 'play' and they dissapear, are they disabled in the hierarchy?
$$anonymous$$y thought was that the items the player has in their inventory would change so I'd re-render it on each open. But it probably would be better to just detect which items need to be added and removed rather than redraw the whole thing.
Removing that loop doesn't change the result. At least not for the first time the inventory opens. The first time the inventory is opened, set of invisible buttons are there. The second time it opens, the visible buttons are added to the grid layout after the invisible buttons.
It's not so much me hitting play and the buttons disappearing so much as me hitting play, opening the inventory, and getting a blank inventory. The invisible buttons that are added do show up in the hierarchy, and the appropriate amount of "Adding new entry" debug logs (line 18 in the code I posted) fire off.