• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
Question by Daedalus216 · Jun 27, 2017 at 10:53 PM · buttoninstantiate prefabonclickdelegatelistener

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";
             }
         }
     }

Comment

People who like this

0 Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image OJEdge · Jun 27, 2017 at 11:02 PM 0
Share

I am positive this is C# (C Sharp) but I will ask you just to be sure. Is this C#?

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

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!

Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image OJEdge · Jun 28, 2017 at 05:55 PM 0
Share

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.

avatar image

Answer by OJEdge · Jun 27, 2017 at 11:00 PM

NameOfButton.onClick.AddListener(MethodName());

Comment

People who like this

0 Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Daedalus216 · Jun 27, 2017 at 11:10 PM 0
Share

Alas, I've tried that too (with and without brackets) and no luck :/

avatar image OJEdge Daedalus216 · Jun 27, 2017 at 11:23 PM 0
Share

What is the full error? you can add me on skype @physc_ for further help.

avatar image Daedalus216 · Jun 28, 2017 at 11:06 AM 0
Share

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 eliminate posibility that the foreach was causing it) but still nothing. No errors and no output

 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;
         cart.transform.parent.FindChild("Checkout").GetComponent<Button>().onClick.AddListener(Clicked);

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

117 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Setting button.onClick.AddListener saids: InvalidCastException: Cannot cast from source type to destination type. 1 Answer

Add listenner to button in loop error 0 Answers

Add listener to multiple buttons 2 Answers

Argument Exception when no arguments? Method Arguments are Incompatible 0 Answers

How to write and return a call for AddListener 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges