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 Primo_Rexus · Dec 11, 2011 at 09:59 PM · guibuttoncolor

Is this code enough to crash Unity?

Okay, so what I'm trying to do with the below code is generate a 4x4 grid of colored buttons that is generated from an array. I don't think I can use the SelectionGrid function for this case, because I can't change the color of the buttons. (It's a window function so it's called from the GUI function.) However, when I run the code, it crashes Unity. Why is this?

 void SavedColorsPanel (int windowID) {
         int i = 0;
         Color curGuiColor = GUI.backgroundColor;
         for(int x = 0;x<4;x++) {
             for(int y = 0;y<4;x++) {
                 if(i<sceneColors.Count) {
                     GUI.backgroundColor = sceneColors[i];
                     GUI.Button(new Rect( y*20+10, x*20+10, 25, 20), "");
                     i++;
                 } else {
                     GUI.backgroundColor = Color.white;
                     GUI.Button(new Rect( y*20+10, x*20+10, 25, 20), "");
                 }
             }
         }
         GUI.backgroundColor = curGuiColor;
         GUI.DragWindow();
     }
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 CHPedersen · Dec 11, 2011 at 10:04 PM 0
Share

You're incrementing x in both the inner and the outer loop. I'm guessing that wasn't intentional. :)

avatar image Primo_Rexus · Dec 11, 2011 at 10:36 PM 0
Share

Aha, that's it. It certainly wasn't intentional! :)

1 Reply

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

Answer by jahroy · Dec 11, 2011 at 10:04 PM

Yes, that code will halt any computer that runs it .

You never change the value of y, so it remains at zero (which is less than 4).

Your for loop will execute forever.

This is an example of an infinite loop.

Comment
Add comment · Show 3 · 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 Primo_Rexus · Dec 11, 2011 at 10:36 PM 0
Share

Yep, that was it. I'd missed the extra X. :)

avatar image Bunny83 · Dec 12, 2011 at 01:02 AM 0
Share

;) typical copy&paste error :D. That's why a lot programmer i know always type in each line. I use copy&paste a lot since i'm not that fast at writing, but i always keep in $$anonymous$$d to check what i've pasted together :D

avatar image CHPedersen · Dec 12, 2011 at 07:55 AM 0
Share

If you use Visual Studio, the editor has nifty little utilities that help you spawn standard code templates so you don't have to copy-paste code. $$anonymous$$icrosoft uses the term "Code Snippet" for them:

http://msdn.microsoft.com/en-us/library/ms165399.aspx

For example, you can write "for" and then hit TAB twice, and it'll spawn a complete for-loop, and if you change the iterator declaration, it updates the iterator reference in the other two references (the stop-condition and the increment) automatically. It's great for stuff like this, where you want an inner for-loop's iterator to be called something else than the outer for-loop's. :)

Code snippets include all common patterns (eg. while, if-else, foreach, switch, etc), and you can create custom snippets too.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Changing gameobject material color makes object disappear from view. How can I fix this? 1 Answer

Change the Colour of UGUI button in script? 2 Answers

guiTexture color half alpha White? 1 Answer

How to have correct color on imGUI Buttons (as dynamic textures) avoiding multiply effect ? 1 Answer

How to change text color on hover in new GUI? 10 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