Here is a function I call out am UI element:
public void LoadCurrentItem(ItemType t)
{
List<Item> itemList = SessionManager.singleton.GetItemAsList(t);
if (itemList == null)
return;
if (itemList.Count == 0)
return;
GameObject prefab = (t == ItemType.consumable) ? eq_left.inventory.slotTemplateUD : eq_left.inventory.slotTemplateLR;
Transform p = eq_left.inventory.slotGrid;
int dif = iconSlotCreated.Count - itemList.Count;
int extra = (dif > 0)?dif: 0;
for (int i = 0; i < itemList.Count + extra; i++)
{
if (i > itemList.Count -1)
{
iconSlotCreated[i].gameObject.SetActive(false);
continue;
}
IconBase icon = null;
if(iconSlotCreated.Count -1 < i)
{
GameObject g = Instantiate(prefab) as GameObject;
g.SetActive(true);
g.transform.SetParent(p);
icon = g.GetComponent<IconBase>();
iconSlotCreated.Add(icon);
}
else
{
icon = iconSlotCreated[i];
}
icon.gameObject.SetActive(true);
icon.icon.enabled = true;
icon.icon.sprite = itemList[i].icon;
icon.id = itemList[i].item_id;
}
}
It might seem less informative, but my question is simple and stupid:
I want to destroy my GameObject g
that instantiates prefab before it instantiates prefab so that I can refresh GameObject g
to create eq_left.inventory.slotTemplateUD
or eq_left.inventory.slotTemplateLR
How can I achieve this goal?