• 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 MINERGUY67880 · Feb 07, 2014 at 05:09 PM · guibuttonskeyboardkeycodefunction update

Is there a way to combine the Update and OnGUI functions?

I need to combine them so that my GUI constantly updates.

 private var IsRKeyPressed : boolean = false;
 private var IsRocksButtonPressed : boolean = false;
 
 function Start () {
 
 }
 
 function Update () {
 
 
 //Is R key being held down
                 if (Input.GetKey (KeyCode.R)) {
                         IsRKeyPressed = true;
         
                 } else {
                         IsRKeyPressed = false;
                 }
                 //Opens Rock Menu
 
 }
 function OnGUI () {
 
 if (IsRKeyPressed == true){
  
  //Rock Category (With rock items/models)
  if (GUI.Button (Rect (0,20,100,100), "Rocks")) {
  IsRocksButtonPressed = true;
    if(IsRocksButtonPressed == true) {
        if (GUI.Button (Rect (200,50,100,100), "Small Rock")) {
       }
 }}}}
 
Comment
Add comment · Show 4
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 robertbu · Feb 07, 2014 at 05:28 PM 0
Share
avatar image MINERGUY67880 · Feb 07, 2014 at 05:33 PM 0
Share
avatar image MINERGUY67880 · Feb 07, 2014 at 07:27 PM 0
Share
avatar image Nanobrain · Feb 07, 2014 at 07:53 PM 0
Share

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by njpatter · Feb 07, 2014 at 07:48 PM

Your code requires that you press the R key to make a GUI.Button show up, w$$anonymous$$ch you will need to continually hold down to get another button to show up... so you will never be able to click the second button because if you let go of the first the second will disappear.

I'm a c# guy, but maybe try somet$$anonymous$$ng like....

       function OnGUI () {
             if (IsRKeyPressed == true){
                 if (GUI.Button (Rect (0,20,100,100), "Rocks")) {
                     IsRocksButtonPressed = true;
                 }
             }
             if(IsRocksButtonPressed == true) {
                 if (GUI.Button (Rect (200,50,100,100), "Small Rock")) {
                     //Do Somet$$anonymous$$ng here
                     isRocksButtonPressed = false;
                 }
             }
         }

T$$anonymous$$s way you don't need to hold the R button and the first GUI button after you have enabled isRocksButtonPressed

Comment
Add comment · Show 2 · 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 Bunny83 · Feb 07, 2014 at 08:20 PM 1
Share
avatar image poncho · Feb 08, 2014 at 05:25 AM 0
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Assign keystrokes to GUI button 2 Answers

gui button to work exctly like up arrow key 1 Answer

Activate GUI Button with Enter Key 1 Answer

How will i develop the type of gui? 0 Answers

My OnGUI() Won't show the Button elements :( 0 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