- Home /
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?");
}
}
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.
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!
Hi what do i add to the code if i want the message to disappear after clicking the object a second time
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!