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 /
This question was closed Oct 07, 2017 at 01:48 AM by Bunny83 for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by Darken · Oct 06, 2017 at 03:23 AM · uibuttonloopdelegateevent-listener

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

Hello, I am trying to assign a function with a parameter to buttons that I create in for loop. I use a delegate to assign it and I make sure to pass a local temp variable as a parameter. For some when I click the button it still calls the function with the reference to the last value of tempint. I have no idea why as I saw countless posts about people using the same thing and it works for them.

     void Start ()
     {
         float activeArea = Screen.height * 0.75f ;
         print(activeArea);
         float buttonDistances = (activeArea) / AudioStreamer.Instance.numberOfStates;
         float currentdist = (activeArea * 0.5f) - buttonDistances;
         float buttonheight = buttonDistances * 0.8f;
 
         for (int i = 0; i < AudioStreamer.Instance.numberOfStates; i++)
         {
             GameObject newbutton = Instantiate(button);
             newbutton.transform.SetParent(gameObject.transform, false);
             newbutton.transform.localPosition = new Vector3(0, currentdist, 0);
 
             Button mybutton = newbutton.GetComponent<Button>();
             mybutton.image.rectTransform.sizeDelta = new Vector2(400, buttonheight);
 
             int tempint = i;
             mybutton.onClick.AddListener(delegate {  CmdSetMusicTo(tempint); }); //<----- This is not working proeprly
 
             Text mytext = newbutton.GetComponentInChildren<Text>();
             mytext.text = MusicFromManager.Instance.l_from_MusicContainers[i].stateName.text;
 
             currentdist -= buttonDistances;
             l_control_Buttons.Add(mybutton);
         }
 
 
 
     public void CmdSetMusicTo(int statenum)
     {
         AudioStreamer.Instance.func_SetMusicState(statenum);
     }

Help is much appricated, 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 Bunny83 · Oct 06, 2017 at 03:32 AM 0
Share

Is that really your actual code? $$anonymous$$eep in $$anonymous$$d that you can't use local temp variables inside a coroutine when you deal with closures as all local variables becomes member variables of the coroutine statemachine object.

The code you've posted should work just fine. So make sure this is really your actual code and tell us what you did to debug the problem. Where did you add Debug.Log statements and what did they return?.

avatar image Darken Bunny83 · Oct 07, 2017 at 12:31 AM 0
Share

Yes that is my actual code. I do not use it in a coroutine I use it in the Start() function. I debugged with attaching a Unity debugger to VS and seeing all the correct variables being passed in the delegate. I also printed all the values of the tempint before it is assigned and showed it correctly

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Darken · Oct 07, 2017 at 12:42 AM

Found the problem, it was passing the correct number but the function inside the Audio Streamer was not handling it correctly. Was looking in the wrong Class.

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

Follow this Question

Answers Answers and Comments

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

Unity UI dynamic Buttons 4 Answers

While loop not updating 1 Answer

Move Button OnClick listeners to another button 3 Answers

Runtime Click Listener Addition 0 Answers

add delegate to toggle in unity 4.6 UI 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