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

We need to see your code to understand why you are having a problem. You can certainly set variables in Update() that are read in OnGUI(). You cannot combine the two functions. Also note that OnGUI() gets called multiple times per frame for different events.

avatar image MINERGUY67880 · Feb 07, 2014 at 05:33 PM 0
Share

I just put in the code. I am trying to make a menu where I can spawn in custom things for a game engine im working on. I can't get the if(IsRocksButtonPressed == true) { if (GUI.Button (Rect (200,50,100,100), "Small Rock")) { } part working. When I click the button, the other button won't pop into the GUI.

avatar image MINERGUY67880 · Feb 07, 2014 at 07:27 PM 0
Share

Can someone please answer? I really need help with this.

avatar image Nanobrain · Feb 07, 2014 at 07:53 PM 0
Share

njpatter,

I think he means for the R button to have to be held down. As referenced in his update function:

 //Is R key being held down

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, which 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 something 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 Something here
                     isRocksButtonPressed = false;
                 }
             }
         }

This 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

I took the liberty and converted your comment into an answer.

I think he wants to have to hold down the R button to show the GUI. The second if (IsRocksButtonPressed) should be inside the first IsR$$anonymous$$eyPressed-if but of course outside of the button-if which is only executed once when you click the button.

avatar image poncho · Feb 08, 2014 at 05:25 AM 0
Share

Nicely done Bunny

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

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