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 /
avatar image
0
Question by Kali2048 · Mar 31, 2015 at 03:08 PM · guibuttoneventinterfacecallback

4.6 UI callback variable

I'm creating buttons using a prefab and instantiate it by code. I'm then trying to get each button to print their own number on click. Right now, each button is only returning the same number "5". Is there an easy way to do that ?

Here is the code


 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class TESTGUI : MonoBehaviour {
 
     public Canvas myCanvas;
     public Button buttonPrefab;
 
     private void printNumber(int myNumber)
     {
         Debug.Log (myNumber);
     }
 
     // Use this for initialization
     void Start () {
         for(int i=0; i<5; i++)
         {
             Button myButton = Instantiate(buttonPrefab) as Button;
             myButton.transform.SetParent(myCanvas.transform, false);
             myButton.transform.position = new Vector3(250f, 50f + (40f * i), 0f);
             myButton.onClick.AddListener(() => printNumber(i));
         }
     }
 }


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 sumeeton · Mar 31, 2015 at 03:49 PM 0
Share

What could possibly be easier or harder way than that? That's the right solution. :)

avatar image Kali2048 · Mar 31, 2015 at 10:37 PM 0
Share

I agree but it's not working :) This is giving each button the same number "5"

1 Reply

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

Answer by DiegoSLTS · Mar 31, 2015 at 10:57 PM

You're setting a lambda function for the onClick event, that function will "know" the value of "i" when you click the button, not when the function is added, and the value will be the value of the "i" variable of the for loop (lambda functions "remember" the context when they where declared).

Since the loop finishes before you can click the buttons, the value of "i" is 5 (the loops goes from i = 0 to i = 4, and it "breaks" once i reaches 5).

The solution is to use another variable that's specific to each iteration instead of the more "global" i variable which is incremented. Replace the last line inside the for loop with this 2 lines:

 int j = i;
 myButton.onClick.AddListener(() => printNumber(j));

This way, for each button, the "j" they know is a different variable from the "j" of the rest.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

GUI.Label positioning for many device resolutions 1 Answer

GUI buttons look fine on full screen but distorted otherwise? 0 Answers

I have doubt please help me 2 Answers

GUI.Button press (not click) 3 Answers

GUI Button to create another GUI 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