I’m trying to make my game menu seamless however I am having a lot of issues when it comes to instantiating buttons. The perfect example I can think of is with my inventory. It takes the information(item id and quantity) in my xml database and puts it into a dictionary that is used to make things happen in real time as the xml database is also updated. For each item in the database there is an instantiated button. My Original goal was that once you click on this button that has the item’s name its description would display and you’d be set to use the item however I wasn’t able to have this new instantiated button affect the gameobjects that weren’t instantiated so I had to create a button to act as a middle man. Is there a way to get around this middle man?
iteminfo: This is what the instantiated button currently does. It gives information to variables which can be used by other buttons to display the description.
**Simply put, How do I go about instantiating this button and having it be able to successfully call the display description function itself without me having to use another function to store the data to call it later? Thanks in advance. **
Here are the variables used followed by the functions as well as a pic of how the set up currently looks:
//Inventory Variables
public GameObject InventoryPanel;
public Text Itemdescription;
public GameObject Itemprefab;
private GameObject Itemslot;
public GameObject Itemcontent;
public GameObject Itemname;
private List<GameObject> itemdestroy = new List<GameObject>();
private static List<string> ItemIDs = new List<string>();
private static List<int> ItemQuantities = new List<int>();
int itemcount;
static int stock;
static string itemdesc;
static string itemid;
public static int itemrefresh;
public Text Itemquant;
Functions:
public void inventorybutton()
{
UnitEquipment.Inventorysetup();
itemrefresh = 1;
backcount++;
itemcount++;
InventoryPanel.SetActive(true);
Inventorydisplay();
}
//Inventory Stuff
public void Inventorydisplay()
{
ypos = -10;
xpos = 0;
foreach (KeyValuePair<string, int> kvp in UnitEquipment.Inventorylist)
{
ItemIDs.Add(kvp.Key);
ItemQuantities.Add(kvp.Value);
Debug.Log(kvp.Value);
}
ItemDatabase it = ItemDatabase.Load("ItemDatabase");
WeaponDatabase wp = WeaponDatabase.Load("WeaponDatabase");
if (itemcount > 1)
{
for (int j = 0; j < itemdestroy.Count; j++)
{
Destroy(itemdestroy[j]);
}
}
for (int i = 0; i <= UnitEquipment.Inventorylist.Count -1; i++)
{
Itemslot = Instantiate(Itemprefab);
itemdestroy.Add(Itemslot);
foreach (BaseEquipment itemit in it.Items)
{
if (ItemIDs *== itemit.ItemID)*
{
Itemslot.name = itemit.ItemID;
Itemslot.GetComponentInChildren().text = itemit.Itemname;
Itemslot.GetComponentInChildren().name = itemit.ItemID;
Itemslot.transform.Find(“Quantity”).GetComponent().text = “x” + ItemQuantities*.ToString();*
}
}
foreach (BaseEquipment itemwp in wp.Weapons)
{
if (ItemIDs == itemwp.ItemID)
{
Itemslot.name = itemwp.ItemID;
Itemslot.GetComponentInChildren().text = itemwp.Itemname;
Itemslot.GetComponentInChildren().name = itemwp.ItemID;
Itemslot.transform.Find(“Quantity”).GetComponent().text = “x” + ItemQuantities*.ToString();*
}
}
Itemslot.transform.SetParent(Itemcontent.transform);
Itemslot.GetComponent().localPosition = new Vector3(xpos, ypos, 0);
ypos -= (int)Itemslot.GetComponent().rect.height;
xpos = 0;
}
}
public void iteminfo()
{
ItemDatabase it = ItemDatabase.Load(“ItemDatabase”);
WeaponDatabase wp = WeaponDatabase.Load(“WeaponDatabase”);
foreach(BaseEquipment item in it.Items)
{
if (Itemname.name == item.ItemID)
{
itemid = item.ItemID;
itemdesc = item.ItemDescription;
}
}
foreach(BaseEquipment item in wp.Weapons)
{
if (Itemname.name == item.ItemID)
{
itemid = item.ItemID;
itemdesc = item.ItemDescription;
}
}
}
public void displayitemdescription()
{
Itemdescription.text = itemdesc;
}
Image: [145450-base.jpg|145450]
[145451-use.jpg*|145451]*
*
*