• 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
Question by sodika · Dec 23, 2013 at 12:55 AM · gui.buttongui.box

OnGui buttons/box leaves a copy or renders twice

I barely started working with Unity and a bug, or misunderstanding, that ive come across twice in the short time is some buttons being permanently being drawn on.

Its my understanding that buttons or boxes are not really objects in the game but are being drawn every frame(twice a frame for OnGui?). But somehow the first thing that I draw stays on the screen even if it doesn't meet some conditionals. Below is a truncated version of my onGui function any help/insight would be appreciated.

 void OnGUI()
     {
         int count = mInventory.Count;
         if (GUI.Button (new Rect (10, 10, 150, 100), "erase")) {
             
             count = 0;
         }
         
         Debug.Log (count);
         for (int x = 0; x < count; x++) 
         {
             //Debug.Log(count);
             if (GUI.Button (new Rect (Screen.width / 2 + (100 * x), Screen.height / 2, 100, 20), "test")) 
             {
                 
                 
             }
             
         }
         
     }

Ive stripped much of my code away to show what I dont understand which is once I click on the erase button, my count should be zero and therefore the "test" buttons shouldnt be rendered but they are.

I have another example where I was displaying health on a guibox but "health 100" always stayed on top and i could see the health drop under that layer. I had a similar thing happen where i put a conditional : if(health < 50) -> dont show anything, but still the health 100 stayed on top and the below layers went away as expected.

Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by robertbu · Dec 23, 2013 at 01:04 AM

OnGUI() gets called multiple time per frame. GUI.Button() will only be true for a single frame, so on the next frame, count will again be set to 'mInventory.Count'. So your code "hides" the 'test' button for a single frame...which won't be noticeable.

To get a better understand of your code, replace the GUI.Button() call with GUI.RepeatButton(). GUI.RepeatButton() returns true as long as the button is held down. Or another way, for test purposes, replace line 6 with:

 mInventory.Count = 0;


Comment

People who like this

0 Show 0 · 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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

How to go to a new gui.window by clicking a gui.button? 1 Answer

GUI.Box doesn't show when GUI.Button is clicked 2 Answers

Why Is GUI.Button Not Drawing GUI.Box 1 Answer

How to make a GUI.Button open/close a GUI.Box? 1 Answer

show/hide GUI.box with a GUI.button 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