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 scarpelius · Apr 02, 2016 at 06:35 AM · guidropdowndelegate

onValueChanged Dropdown in loop

I am having a problem with dropdowns which I add them programatically, more precise with the onValueChanged event. No matter which dropdown value I change in game the parameter used by onValueChanged is the last row instantiated.

Any idea?

 int inc = 0;
 foreach (Report report in reports)
 {
     inc++;
     GameObject row = Instantiate(reportRow) as GameObject;
     row.name += " - " + report.id;
     row.transform.parent = rows;
     row.AddComponent<Report>().SetValues(report);
     row.transform.Find("Status").GetComponent<Dropdown>().onValueChanged.AddListener(delegate { StatusChange(row.transform); });
 }


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
0

Answer by Bunny83 · Apr 02, 2016 at 02:16 PM

Uhm, that shouldn't happen in your case since you already are using a local variable (row) that is declared inside the foreach loop. So the closure you're creating should close around the current "row" variable. Are you sure that this is your exact code?

I'm a bit confused. What exactly is "reportRow"? Isn't it a prefab? Why doesn't it already contain a Report script? By adding the report script directly to your prefab you could do a lot of those initialization inside the Report script. You can also setup the required links in the inspector. By adding a public Dropdown variable to the Report script you can simply setup the link to the dropdown field in the editor.

Next thing that seems a bit strange is, if Report is a Component (MonoBehaviour), where are those report scripts attached to that are referenced in the "reports" array / collection? Keep in mind that you shouldn't create MonoBehaviours with "new". To initialize a component with values from another component of the same type seem a bit odd.

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
avatar image
0

Answer by scarpelius · Apr 03, 2016 at 07:53 AM

Thank you for your response. Report is a class that maps a table in DB. I've changed the code to remove the new assignment to a MonoBehaviour class. Same behavior unfortunately :( Here is the new code and the report class:

 var r = JSON.Parse(result.text);
 int nr = r["rows"].Count;
 
 Transform rows = gameMySuggestionsWindow.Find("Table").Find("Rows Scroll View").Find("Viewport").Find("Content");
 for (int i = 0; i < nr; i++)
 {
     GameObject row = Instantiate(reportRow) as GameObject;
     row.name += " - " + r["rows"][i]["id"];
     row.transform.parent = rows;
     row.AddComponent<Report>().SetValues(r["rows"][i]);
     row.transform.localPosition = new Vector3(0, -i * 60, 0);
     Report report = row.GetComponent<Report>();
     row.transform.FindChild("Number Text").GetComponent<Text>().text = i.ToString();
     row.transform.FindChild("Level Text").GetComponent<Text>().text = GameManager.Instance.scenes[report.mapId];
     row.transform.FindChild("Type Text").GetComponent<Text>().text = report.reportType.ToString();
     Transform desc = row.transform.Find("Scroll View").Find("Viewport").Find("Content").Find("Description Text");
     desc.GetComponent<Text>().text = report.description;
     row.transform.FindChild("Status").GetComponent<Dropdown>().value = reportStatus[report.status];
     row.transform.Find("Status").GetComponent<Dropdown>().onValueChanged.AddListener(delegate { StatusChange(row.transform); });
 }

and the Report class

 public class Report : MonoBehaviour
 {
     public int id;
     public Vector3 characterPosition;
     public string reportType;
     public string description;
     public string status;
     public string dateAdded;
 
     public void SetValues(JSONNode r)
     {        
         id = Convert.ToInt32(r["id"]);
         string[] pos = Convert.ToString(r["character_position"]).Split(',');
         if (pos.Length > 0)
         {
             characterPosition = new Vector3(Convert.ToSingle(pos[0]), Convert.ToSingle(pos[1]), Convert.ToSingle(pos[2]));
         }
         else
         {
             characterPosition = Vector3.zero;
         }
         reportType = Convert.ToString(r["report_type"]);
         description = Convert.ToString(r["description"]);
         status = Convert.ToString(r["status"]);
         dateAdded = Convert.ToString(r["date_added"]);
     }
 }
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

60 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

Related Questions

GUI Popup problem…! 1 Answer

How to make a GUI Box drop down menu? 1 Answer

Dropdown menu selection 0 Answers

How to initialize a boolean only once when used in a function when called OnGUI 1 Answer

How to make a list of Actions or methods? 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