How to use Button.OnClick.AddListener?
Hey guys, so I've been struggling with this one for hours and I can't for the life of me figure it out.
The code is intended to populate the shops 'offers' area with the items it has in stock, deciding weather to make a new item or increase the quantity of the item if its already listed. Each item (which is instantiated from a prefab) has a button attached 'add to cart', however regardless of what i put in the listener, whether i use delegate or ()=> or just call a simple function like below, it doesnt work. It doesnt throw any errors either, it just simply does nothing. The buttons appear to be clicking, no raycast is blocked or anything as far as I can tell. Any suggestions?
public GameObject inventory;
public GameObject shop;
public string storeName;
public List<Item> stock;
public GameObject offers;
public GameObject cart;
public int money;
public GameObject itemPrefab;
// Use this for initialization
void Start () {
shop.SetActive(false);
offers = shop.transform.FindChild("Buy Window").FindChild("Offers Window").FindChild("Offers").gameObject;
cart = shop.transform.FindChild("Buy Window").FindChild("Cart Window").FindChild("Cart").gameObject;
}
void Clicked()
{
Debug.Log("Clickety click");
}
public void OpenShop(string shopKeeper)
{
inventory.GetComponent<InventoryToggle>().ShopToggle(true);
shop.GetComponent<ShopInventories>().GetShopData(shopKeeper);
shop.transform.FindChild("Shop Name").FindChild("Text").GetComponent<Text>().text = storeName;
shop.transform.FindChild("ShopKeeper").FindChild("Name Panel").FindChild("Text").GetComponent<Text>().text = shopKeeper;
foreach (Item item in stock)
{
bool newItem = true;
int stackLocation = 0;
int quantity = 1;
int stackSize = 1;
for (int i = 0; i < offers.transform.GetChildCount(); i++)
{
if (offers.transform.GetChild(i).FindChild("Name").GetComponent<Text>().text == item.Title)
{
newItem = false;
stackLocation = i;
break;
}
}
//Check if stacking is possible
if (newItem == true)
{
GameObject newOffer = Instantiate(itemPrefab);
newOffer.transform.SetParent(offers.transform);
newOffer.transform.FindChild("Name").GetComponent<Text>().text = item.Title;
newOffer.transform.FindChild("Slot").FindChild("Item").GetComponent<Image>().sprite = item.Sprite;
newOffer.transform.FindChild("Quantity").GetComponent<Text>().text = quantity + "x";
newOffer.transform.FindChild("Value").GetComponent<Text>().text = "Price: " + item.Value.ToString();
Button addToCart = newOffer.transform.FindChild("Add To Cart").GetComponent<Button>();
addToCart.onClick.AddListener(delegate { Clicked(); });
}
else
{
string newQuantity = offers.transform.GetChild(stackLocation).FindChild("Quantity").GetComponent<Text>().text;
quantity = Int32.Parse(newQuantity.Substring(0, newQuantity.Length-1));
quantity++;
offers.transform.GetChild(stackLocation).FindChild("Quantity").GetComponent<Text>().text = quantity+"x";
}
}
}
I am positive this is C# (C Sharp) but I will ask you just to be sure. Is this C#?
Answer by Daedalus216 · Jun 28, 2017 at 11:17 AM
Deleting the button on the prefab and adding it again, then restarting unity seems to have solved the problem for me, at least for now. Hopefully that will be the end of it!
find me by email maybe? oj.edge@outlook.com, and okay great if you have any more issues let me know, maybe you could help me too.
Answer by OJEdge · Jun 27, 2017 at 11:00 PM
NameOfButton.onClick.AddListener(MethodName());
Alas, I've tried that too (with and without brackets) and no luck :/
What is the full error? you can add me on skype @physc_ for further help.
I can't find you on skype -_- Anyone able to shed light on the problem? Just tried the code below (used simplest logic outside the for loop to eli$$anonymous$$ate posibility that the foreach was causing it) but still nothing. No errors and no output
public void OpenShop(string shop$$anonymous$$eeper)
{
inventory.GetComponent<InventoryToggle>().ShopToggle(true);
shop.GetComponent<ShopInventories>().GetShopData(shop$$anonymous$$eeper);
shop.transform.FindChild("Shop Name").FindChild("Text").GetComponent<Text>().text = storeName;
shop.transform.FindChild("Shop$$anonymous$$eeper").FindChild("Name Panel").FindChild("Text").GetComponent<Text>().text = shop$$anonymous$$eeper;
cart.transform.parent.FindChild("Checkout").GetComponent<Button>().onClick.AddListener(Clicked);