I have a UI Prefab, an inventory GUI that I pull from a pool Instantiating as needed. I also Instantiate Children of the Inventory GUI to represent buttons, images, etc… However when I instantiate the prefab and need to create a new one (do to an empty pool) it does not add the children UI elements to the parent.
Here is sample code
public static GameObject AllocateInventoryGUI() {
GameObject newInventoryGUI;
if (Instance.InventoryPanels.Count > 0) {
newInventoryGUI = Instance.InventoryPanels.Dequeue ();
} else {
newInventoryGUI = GameObject.Instantiate (Instance.InventoryPrefab);
newInventoryGUI.transform.SetParent (Instance.Canvas, false);
}
newInventoryGUI.SetActive (true);
return newInventoryGUI;
}
void PopulateInventoryGUI() {
InventoryGUI = InventoryManager.AllocateInventoryGUI();
for(Dictionary<ItemKey, Item>.Enumerator iter = Inventory.GetEnumerator(); iter.MoveNext();) {
ItemKey currentKey = iter.Current.Key;
Item currentItem = iter.Current.Value;
Transform currentGrid = InventoryGUI.transform.GetChild(currentKey.Container);
GameObject ItemIcon = InventoryManager.AllocateInventoryIcon ();
ItemIcon.SetActive(true);
//ItemIcon.transform.SetParent (currentGrid);
//ItemIcon.GetComponent<RectTransform> ().localScale = Vector3.one;
ItemScript currentScript = ItemIcon.GetComponent<ItemScript> ();
currentScript.SetupItem (currentItem);
IItemContainer container = currentGrid.GetComponent<IItemContainer> ();
container.StoreItem (ItemIcon, currentKey.Position, currentItem, true);
}
}
It works perfectly if I preinstantiate the newInventoryGUI before using it in the pool.
Is this just to do with Unity Instantiation or is there a workaround?