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 rohsabman · Jun 06, 2012 at 09:28 PM · gui.buttongui.box

Create a GUI on mouse click

Hi!

I was trying to create a GUI box upon the click of the mouse. If I try to print a sentence, or pause time, that works fine. However, I cannot get a box to be displayed. Below is the segment of the code I have for it (crossPenalty and stringToEdit are declared at the beginning of this script). This code is in my OnGUI() function. I've been searching through forums to find guidance but nothing works still... I would love my advice with this problem.

Thank you!! Ro

    if(crossPenalty)
    {
     
     var msgtxt = "What is the smallest prime number?";
     // Show message in GUI box when player enters the penalty area
     //GUI.Box ( Rect( 200, 100, 320, 110),msgtxt);
     
     // Make a text field that modifies stringToEdit
     stringToEdit = GUI.TextArea (Rect(200, 220, 300, 50), stringToEdit, 200);
     
     if(GUI.Button(Rect(500, 222, 60, 45), "Submit"))
     {
         //Time.timeScale = 0.000001;
         //GUI.Box ( Rect( 200, 250, 320, 70), "HERE");
         print ("You clicked the button!");
         GUI.Box ( Rect( 200, 100, 320, 110),msgtxt);
         //print ("Where is the box?");
     }

 
 }
Comment
Add comment
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

2 Replies

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

Answer by FishSpeaker · Jun 06, 2012 at 10:04 PM

GUI.Button only returns true on the single frame that the player releases the button.

If you want the Box to appear when the player presses the button and stay up until some later time that you handle elsewhere, you can do this:

 var submitPressed = false;
 function OnGUI() {
     var msgtxt = "What is the smallest prime number?";
     if( GUI.Button( Rect( 500, 222, 60, 45 ), "Submit" ) ) {
         print ("You clicked the button!");
         submitPressed = true;
     }
     if( submitPressed ) {
         GUI.Box( Rect( 200, 100, 320, 110 ), msgtxt );
     }
 }

If you want the message to be displayed only while the player is holding the button down, you can do this:

 function OnGUI() {
     var msgtxt = "What is the smallest prime number?";
     if( GUI.RepeatButton( Rect( 500, 222, 60, 45 ), "Submit" ) ) {
         print ("You clicked the button!");
         GUI.Box( Rect( 200, 100, 320, 110 ), msgtxt );
     }
 }

Note that "You clicked the button!" is being printed every frame in this case.

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 rohsabman · Jun 06, 2012 at 10:24 PM 0
Share

Thank you sooooo much!!! I didn't realize GUI.Button only returned true for just that one frame! $$anonymous$$akes complete sense though. Thank you again!

avatar image gazza529 · May 07, 2013 at 12:26 PM 0
Share

Hi what do i add to the code if i want the message to disappear after clicking the object a second time

avatar image
0

Answer by rohsabman · Jun 06, 2012 at 11:51 PM

Thank you sooooo much!!! I didn't realize GUI.Button only returned true for just that one frame! Makes complete sense though. Thank you again!

Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

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

OnGui buttons/box leaves a copy or renders twice 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


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