Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by Guennor · May 09, 2016 at 07:35 AM · uibuttonbuttonsonclick

Runtime UI button creation: can't make an "OnClick" where I call a function from another script while sending a parameter.

I've looked around and even though there were people with similar problems to this one, none of the solutions could help my case.

What I want to do is to create multiple buttons at runtime, all of the buttons will call the same function on another script, but will provide different parameters (gameObjects).

So this is the code:

 for (int i = 0; i < cockpits.Count; i++) 
         {
             //Create Buttons
             GameObject b = GameObject.Instantiate (buttonPrefab);
             b.transform.SetParent (cockPitsRect.transform, false);
             b.transform.localScale = new Vector3 (1, 1, 1);
             Text t = b.GetComponentInChildren<Text> ();
             t.text = cockpits [i].name;
 
             Button tempButton = b.GetComponent<Button> ();
 
             tempButton.onClick.AddListener(() => shipScript.AddPart(cockpits[i]));
 
             RectTransform buttonRect = tempButton.GetComponent<RectTransform> ();
             b.name = cockpits [i].name;
             buttonRect.anchoredPosition = new Vector2 (0, 0.5f);
             buttonRect.localPosition = new Vector2(cockPitsRect.sizeDelta.x /2 *-1 + buttonRect.sizeDelta.x/2 + i*100,0);
             cockpitButtons.Add (b);
 
         }//For

And this is the part that is causing trouble:

 tempButton.onClick.AddListener(() => shipScript.AddPart(cockpits[i]));

I mean, it should be simple, right? I call the function "AddPart" on the "shipScript" script, and provide the parameter which is the gameobject [i] on the list of gameobjects "cockpits".

But it doesn't work. The game compiles just fine, but when I try to click on any of the 3 buttons, this appears on the console:

ArgumentOutOfRangeException: Argument is out of range

So I tried debbuging it and turns out that the "i" value, even though it should be 0, 1, and 2 for the first, second and third buttons respectively, becomes "3" when I press the button (that is why it becomes out of range I guess?).

So I really have no idea how can I make this work.

Thanks in advance! :)

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by M-Hanssen · May 09, 2016 at 08:37 AM

Try setting the cockpit variable to a local variable before inserting it into the lambda expression like this:

 //(I don't know the exact type of the cockpits array value, so change if necessary)
 Cockpit cockpit = cockpits[i]; 
 tempButton.onClick.AddListener(() => shipScript.AddPart(cockpit));

Comment
Add comment · 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 Guennor · May 09, 2016 at 05:15 PM 0
Share

This is it. It worked! Unity answers is awesome. Thank you a lot sir!

avatar image
2

Answer by Ryanless · May 09, 2016 at 09:27 AM

yep, i had the same problem once. This is how i solved it. Pretty similar to Hanssen, though i gess making a local int is slighlty less cpu demanding than making new local cockpit variables ;-).

 int t = i;
  tempButton.onClick.AddListener(() => shipScript.AddPart(cockpits[t]));
Comment
Add comment · 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 Guennor · May 09, 2016 at 05:17 PM 0
Share

I tested and this also works! Thank you for the help! I spent hours stuck with this problem.

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.

Follow this Question

Answers Answers and Comments

64 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

Related Questions

How to ADD one Buttons.onClick events to another Buttons.onClick? 1 Answer

button.onClick.AddListener(method); NOT Working 1 Answer

Functions aren't appearing in the onClick editor..... 1 Answer

Creating working UI Button from Script? 1 Answer

This is not possible to be called for standalone input. Please check your platform and code where this is called (C#) 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