Question by
ritoban · Aug 16, 2016 at 04:36 PM ·
uiinstantiatebuttoninventory
Buttons in one panel affecting another.
I have 2 panels, which are being dynamically filled with buttons. The first is filled like this:
public GameObject m_button;
public ItemPanelButtons itemPanelButtons;
// Use this for initialization
void Start ()
{
foreach(var type in System.Enum.GetValues(typeof(InventoryItemType)))
{
GameObject button = (GameObject)Instantiate(m_button, Vector3.zero, Quaternion.identity);
button.transform.SetParent(transform, false);
string typeStr = type.ToString();
//Add space before each uppercase letter
string output = System.Text.RegularExpressions.Regex.Replace(typeStr, "([a-z])_?([A-Z])", "$1 $2");
button.transform.FindChild("Text").GetComponent<Text>().text = output;
button.GetComponent<Button>().onClick.AddListener(() => itemPanelButtons.AddItemButtons(typeStr));
}
}
The second is filled when the using the itemPanelButtons.AddItemButtons()
function.
public InventoryManager inventory;
public GameObject m_button;
public RectTransform itemPanel;
public void AddItemButtons(string type)
{
for(int i = 0; i < inventory.inventory.Count; i++)
{
if(inventory.inventory[i].m_category.ToString() == type)
{
GameObject button = (GameObject)Instantiate(m_button);
button.transform.SetParent(itemPanel, false);
button.transform.FindChild("Text").GetComponent<Text>().text = inventory.inventory[i].m_name;
}
}
}
When type button is pressed, and the inventory array is empty or there is nothing of that type in the inventory, as expected. However, if a button is added to the item panel, or even if I start the game with a button already in the item panel, the buttons in the type panel become disabled. They do not show the disabled color, but clicking them has no effect. I can even print a string using them.
The entire project can be accessed from here: https://www.dropbox.com/sh/egx4nft9o6hkmnd/AAAKL-CKGOGnpSPL3RwQol8ga?dl=0.
Thank you in advance.
Comment