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 Klaes4Zaugen · May 16, 2015 at 01:33 PM · c#foreachguibutton

Not updating value in foreach loop OnGUI()

I can't get some a List to update inside a foreach loop inside a GUI.Button call. The actual value is correct using Debug.Log(value) but not on the GUI

Code:

 foreach (var element in categoriesArray)
     {
     if ( !element.Toggled )
     {
         if ( GUI.Button(new Rect(15,185,170,185), element.ToggledString ) )
         {
             Debug.Log("pressed on");
             element.Toggled = true;
             element.ToggledString = "[X]";
             Debug.Log(element.ToggledString);
         }
     }
     else if ( element.Toggled )
     {
         if ( GUI.Button(new Rect(15,185,170,185), element.ToggledString ) )
         {
            element.Toggled = false;
            element.ToggledString = "[ ]";
            Debug.Log("pressed off");
            Debug.Log(element.ToggledString);
         }
     }
 }


http://i.gyazo.com/13059b192407cb1e5fe4e1adecad97ba.png

Comment
Add comment · Show 5
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 AlwaysSunny · May 16, 2015 at 01:29 PM 0
Share

Any good reason you're not using UI? Unless you're editor scripting, you should be using the newer UI system.

I don't understand the language of your question, but the only thing I spy "wrong" with this code is that it could be about 15 lines shorter and achieve the same thing. Also, if you're making a list of buttons, shouldn't their x or y position involve a for-loop's iterator?

Oh wait, I get the question now. The element's text is not updated to reflect the change to element.ToggledString, right? Okay, I couldn't figure it out in 5 seconds, so I'll publish the question.

avatar image Klaes4Zaugen · May 16, 2015 at 08:45 PM 0
Share

I prefer script UI to be honest. Yeah the element's text is not updated visually but doing a Debug.Log(value) prints the updated value.

avatar image maccabbe · May 16, 2015 at 09:04 PM 0
Share

You are going through each element in categoriesArray and drawing all of them in the same rect (15,185,170,185). Have you tested if some of the GUI is changing but you can't see it since even one "[X]" in the array might cover all elements with "[ ]"?

avatar image AlwaysSunny · May 16, 2015 at 09:09 PM 1
Share

edit: yeah, like @maccabbe says, and I mentioned earlier, "if you're making a list of buttons, shouldn't their x or y position involve a for-loop's iterator?"

All I can think of is that the change wouldn't happen until the next frame. If it doesn't happen at all, perhaps some code we aren't seeing is responsible.

You could compress this down into a single button. Not saying it will help, but it is cleaner. It's conceivable that your conditionals are to blame for this, but I'll admit it seems unlikely.

 if (GUI.Button(new Rect(15,185,170,185), element.ToggledString )) {
   element.Toggled = !element.Toggled;
   element.ToggledString = (element.Toggled) ? "[X]" : "[ ]";
 }



avatar image Klaes4Zaugen · May 16, 2015 at 09:39 PM 0
Share

It was because I rendered it over the same pos over and over again. I only made a non for loop pos for testing.

Thanks

0 Replies

· Add your reply
  • Sort: 

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# - foreach problem 3 Answers

Failure to destroy a game object in a for each loop 1 Answer

Find greatest distance between a List of objects? 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