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 /
  • Help Room /
avatar image
0
Question by yosub0121 · Oct 17, 2016 at 07:24 AM · for loop

what is wrong with my for loop?

Hi I'm new to unity3D and really enjoying creating what I want to make. But while making the friend request accept/refuse system, I'm struggling with with a for loop which adds listeners to UI Buttons. During runtime, every Buttons are having the last listener. for example, if for loop conditions were (int i=0; i<5; i++), and when I debug a listener attached to the button, the button always have fifth button listener that isn't in the condition. Here is my script, and sorry for my short English.

     //start from here
     void Awake ()
 {
     StartCoroutine (CheckingRequest ());
     username.text = PlayerPrefs.GetString("username");
 }

      //gets username from Mysql Database, and split that for buttons
 IEnumerator CheckingRequest ()
 {
     WWWForm form = new WWWForm ();
     form.AddField ("userNamePost", PlayerPrefs.GetString("username"));
     WWW www = new WWW (CheckingRequestUrl, form);
     yield return www;
     Debug.Log (www.text);
     requestNum = www.text.Split ('|').Length - 1;
     Debug.Log (requestNum);
     testString = www.text;

             //here is problem area 

     for (int i = 0; i < requestNum; i++) 
     {
         friendRequest [i].text = www.text.Split ('|') [i];
         Debug.Log (i);

         acceptButton [i].onClick.AddListener (() => AcceptRequestWrapper (i));
     }
 }

 
 IEnumerator AcceptRequest (string friendRequesting)
 {
     
     WWWForm form = new WWWForm ();
     form.AddField ("userName", PlayerPrefs.GetString("username"));
     form.AddField ("actionUser", friendRequesting);
     WWW www = new WWW (AcceptRequestUrl, form);
     yield return www;
     Debug.Log (www.text);
 }

 public void AcceptRequestWrapper (int e)
 {
     Debug.Log (e);
     StartCoroutine (AcceptRequest (friendRequest[e].text));
     Debug.Log (friendRequest [e].text);
     Debug.Log ("Button Clicked");
 }
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

3 Replies

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

Answer by NoseKills · Oct 18, 2016 at 04:25 PM

      for (int i = 0; i < requestNum; i++)  {
          acceptButton [i].onClick.AddListener (() => AcceptRequestWrapper (i));
      }

When using a lambda like this, the actual variable (`i`) gets wrapped into the closure, not the value of it.

In other words, when you finally press the button and AcceptRequestWrapper gets called, the loop has already finished and the value of i is indeed what it is when the loop finishes.

You should be able to easily avoid this by giving the lambda a new int you dont change in the loop.

 for (int i = 0; i < requestNum; i++)  {
          var tempI = i;
           acceptButton [i].onClick.AddListener (() => AcceptRequestWrapper (tempI));
  }
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 yosub0121 · Oct 20, 2016 at 10:08 AM 0
Share

thanks, that would work...!!

avatar image yosub0121 · Oct 20, 2016 at 10:37 AM 1
Share

that worked! really thanks!!!!!!!

avatar image
0

Answer by Dave29483 · Oct 17, 2016 at 07:44 AM

In the example you provided

(int i=0; i<5; i++)

This loop will run 5 times, not four. If your loop started at 1, it would run 4 times.

The zero index counts as a loop too.

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 yosub0121 · Oct 18, 2016 at 01:13 AM 0
Share

Thank you for a fast replying. Then, how can I abbreviate that process? Is there any appropriate way to shorten the line? Should I write all codes regardless of the length?

avatar image Dave29483 · Oct 18, 2016 at 07:12 AM 0
Share

How are you building your acceptButton array?

avatar image
0

Answer by Zodiarc · Oct 18, 2016 at 10:51 AM

Use a foreach loop then you won't need to deal with array sizes (pseudocode):

 foreach(FriendRequest friendRequest in friendRequests) {
     // do your stuff here
 }
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

77 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 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

For Loop not working 1 Answer

For loop only detecting duplicates of the first element in a list of GameObjects 1 Answer

Ending a loop and returning to the previous loop 1 Answer

simplest way to add a minimum distance or gap between instantiated objects 1 Answer

for loop not working 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