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