Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
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
Add comment
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
1
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
Add comment · 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.

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

GUI.Button press (not click) 3 Answers

GUI Button to create another GUI 1 Answer

Stop clicking through a GUI window 2 Answers

Closing/hiding an open Gui Button when clicking on it 1 Answer

Instantiating object from inventory 0 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