• 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 /
This question was closed Jun 28, 2015 at 03:12 PM by DeadKenny for the following reason:

The question is answered, right answer was accepted

avatar image
Question by DeadKenny · Jun 28, 2015 at 02:26 PM · guiinstantiatebuttoninventoryclick

New GUI and Inventory problem.

Ok so basically what I have is an inventory with buttons representing objects and when you click on said button I need it to get said object so I can instantiate and do whatever else. However I am getting an argument out of range error after adding the listener to the GUI button.

I have only resently moved to the new GUI from unity 4.6 so I need help to figure it out all over again.

Here is the code that sets the listener to the GUI button in the inventory. It works as the debug registers but it won't assign the gameobject.

     private GameObject eqObject; //the Object that is referenced;
     
                 for(int cnt = 0; cnt < plyrPlayerScript.CurrentInventoryList.Count; ++cnt){
 //The button instantiated for each object.
                     GameObject slt = (GameObject)(Instantiate(ivntSlot));
                     slt.transform.parent = guiIvnt.transform.Find("MaskBox").transform;         
                     //Set Onclick:
                     slt.GetComponent<UnityEngine.UI.Button>().onClick.AddListener(() => {
                         //Ok this works but you need graphics raycaster component on the parent of the buttons otherwise no click1!!!
                         Debug.Log("Works!!!!!!");
     //This is the part that causes the out of range error. What am I not assigning or doing wrong? 
                         eqObject = plyrPlayerScript.CurrentInventoryList[cnt].transform.gameObject;
                         
                     });
     
                     //
                 }    
     
     
     //Thanks.
 
Comment

People who like this

0 Show 0
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

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by jenci1990 · Jun 28, 2015 at 02:58 PM

I think the problem is the delegate void. When you log 'works', please log the 'cnt' varible too. I think it is always count+1. Try make a new function to add listener to button. I think this will be solve your problem:

     for(int cnt = 0; cnt < plyrPlayerScript.CurrentInventoryList.Count; ++cnt) {
         GameObject slt = (GameObject)(Instantiate(ivntSlot));
         slt.transform.parent = guiIvnt.transform.Find("MaskBox").transform;
         AddButtonListener(slt, cnt);
     }
     
     
 void AddButtonListener(GameObject _slt, int _index) {
     _slt.GetComponent<UnityEngine.UI.Button>().onClick.AddListener(() => {                       
         eqObject = plyrPlayerScript.CurrentInventoryList[_index].transform.gameObject;
     });
 }
 
 
 
 
 
 
 

Comment
DeadKenny

People who like this

1 Show 2 · 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 DeadKenny · Jun 28, 2015 at 03:00 PM 0
Share

Ok will try it thanks.

avatar image DeadKenny · Jun 28, 2015 at 03:09 PM 0
Share

Awsome it works. Thanks bro.

Helped me understand a lot more of how the new GUI works.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

proper inventory system issue.. 1 Answer

how to change a gui button texture 1 Answer

Issues with Inventory Code (Fixed!!) 0 Answers

Bullet Prefab not instantiating 1 Answer

gui.button down 2 Answers


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