Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
4
Question by stulleman · Jan 13, 2015 at 01:28 PM · uibuttondelegate

Unity UI dynamic Buttons

Hello Forum,

I'm trying to make a menu that is built at runtime.

         for (int i = 0; i < bd.ItemCount(); i++) {
 
             buttons.Add((GameObject)Instantiate(button));
             buttons[i].transform.SetParent(transform, false);
             buttons[i].transform.GetChild(0).GetComponent<Text>().text = bd.GetNameById((byte)i);
 
             Vector3 position = buttons[i].GetComponent<RectTransform>().position;
             position.y -= 40 * i;
             buttons[i].GetComponent<RectTransform>().position = position;
 
             buttons[i].GetComponent<Button>().onClick.AddListener(() => {
                 builder.SelectItem(buttons[i].transform.GetChild(0).GetComponent<Text>().text);
             });
         }

You can ignore the first part. I can imagine why this isn't working but I have no better idea. The problem is that the call to the method is when the i does not exist anymore. How can I know which button called the method?

Thanks!

Comment
Add comment · Show 2
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 graspee · Jan 11, 2016 at 03:23 PM 0
Share

The bug is not fixed yet. (January 2016, version 5.3).

avatar image Alexander21 · Mar 23, 2016 at 02:07 PM 0
Share

Hi All I have also looking for the code. First time i have run the code. It works perfect. But after that i have try to run the above code. It shows the error...

NullReferenceException: Object reference not set to an instance of an object

It does not works.. How can i solve it......

2 Replies

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

Answer by Mmmpies · Jan 13, 2015 at 06:06 PM

Try creating them like this, there's an issue with the way C# hold references to variables but I can get the buttons to Debug.Log the button number when I create them like this and set the Lambda using temp variables. I believe the issue is fixed in 5 but not used 5 yet so can't be 100% sure:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class LambdaButtons : MonoBehaviour {
 
     public GameObject prefabButton;
     public RectTransform ParentPanel;
 
     // Use this for initialization
     void Start () {
 
         for(int i = 0; i < 5; i++)
         {
             GameObject goButton = (GameObject)Instantiate(prefabButton);
             goButton.transform.SetParent(ParentPanel, false);
             goButton.transform.localScale = new Vector3(1, 1, 1);
 
             Button tempButton = goButton.GetComponent<Button>();
             int tempInt = i;
 
             tempButton.onClick.AddListener(() => ButtonClicked(tempInt));
         }
 
     
     }
 
     void ButtonClicked(int buttonNo)
     {
         Debug.Log ("Button clicked = " + buttonNo);
     }
 
 }

Also sorry it took so long, I saw this at lunch time but was at work for the afternoon. You'll need a vertical layout group on the panel you put them in so they don't build on top of each other. I did that and put a layout element on the button prefab with a minimum height of 30, I also stopped the expand on height in the panels vertical group. This is the result I get:

LambdaButtons


lambdabuttons.png (32.0 kB)
Comment
Add comment · Show 4 · 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 stulleman · Jan 15, 2015 at 01:03 PM 0
Share

Thank you very much! Hope they fix this, as this seems not very intuitive to me. Anyway thanks for your help, better late then never :P

avatar image Icarus619 · Apr 12, 2016 at 05:31 PM 0
Share

Hi, somehow all five buttons are rendered above each other and not on the panel. What am i doing wrong here? I assigned a Panel in the upper left corner with a decent size, but all buttons are rendered somewhere on the screen..

avatar image Laaevin · Jul 13, 2017 at 09:39 PM 0
Share

Sorry to necro this. I'm trying to accomplish this same task only in Javascript and I'm getting stuck where your code is on line 22. When I try to call a function with this line of code:

tempButton[tempInt].GetComponentInChildren.().onClick.AddListener(ButtonClicked(tempInt));

It says: "No appropriate version of UnityEngine.Events.UnityEvent.AddListener' for the argument list '(void)' was found."

Can anyone help me figure out how to have each button call a function with a different input?

avatar image gobyfazakas · Jul 13, 2020 at 09:02 AM 0
Share

Thanks a lot, if you ever read this you helped me, and many others who didn't bother commenting

avatar image
1

Answer by WarpZone · Jan 16, 2017 at 03:29 PM

@Icarus619 Like he said in his answer, you'll need a vertical layout group on the panel you put them in so they don't build on top of each other.

Here's a youtube video that will walk you through adding a vertical layout group to your items: https://www.youtube.com/watch?v=lUun2xW6FJ4

It also tells you how to implement a scrollbar, in case your list gets taller than the screen.

Good luck.

Comment
Add comment · 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

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

34 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

Related Questions

Assigning a function with a parameter to buttons in for loop with a temp variable 1 Answer

Problem with onClick.AddListener 1 Answer

add delegate to toggle in unity 4.6 UI 2 Answers

Setting Button.onClick with a script in the editor 1 Answer

Runtime Click Listener Addition 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