- Home /
text fields and GUI
/*Heyy
I have a program here which many of you will find useful but there is one problem .
okay , in this script there is a tag i created called "xclpanel" , whenever a character steps on the object tagged with xclpanel a GUI window will appear which will generate a question to the player . I have created a text field but i have not fixed it to react to the user pressing the enter button after inputting an answer . I the main problem is that whenever the xclock is true ( the character has stepped on the panel) the Text field does not appear when xclock is set to false at the END of the function to generate the windows .
could you please help me to get the text field to appear with the question when the panel is stepped on and disappear when the character moves off the panel
and how would i go about creating an answer for the question generated so that when the user presses the enter button the answer is checked :s
Here is my Java script *
*/
var doWindow : boolean = true;
private var xclock = false;
var stringToEdit : String = "";
function OnControllerColliderHit (hit: ControllerColliderHit)
{
if(hit.gameObject.tag == "xclpanel")
{
xclock= true; // character stepped on xclock
}
}
function DoWindow (windowID : int) {
}
function OnGUI ()
{
if (xclock)
{
// GUI DISPLAYED IF PANEL IS HIT
GUI.color = Color.yellow;
GUI.backgroundColor = Color.yellow;
GUI. Window (0, Rect (1,40,300,140), DoWindow, " what is english Language and why is it important ");
// THIS IS THE TEXT FIELD
stringToEdit = GUI.TextField (Rect (1, 180, 300, 50), stringToEdit, 200);
xclock = false ; // the TEXT FIELD WILL NOT APPEAR IF THIS IS NOT OMITTED ( THIS IS THE PROBLEM)
}
}
Could you please format your code properly? Highlight the whole code and press the 101010 button in the toolbar.
Answer by GuyTidhar · Apr 08, 2012 at 07:47 AM
First thing: Remove the:
xclock = false;
From the OnGUI() function. What you basically did is once you display the clock - you hide it.
2nd, add the following function to the same script:
function OnCollisionExit(collisionInfo : Collision)
{
if ( collisionInfo.collider.tag == "xclpanel" )
{
xclock = false;
}
}
By this code, you are telling the controller, to hide the clock once you 'exit' the collider tagged 'xclpanel'.
Your answer
![](https://koobas.hobune.stream/wayback/20220613070043im_/https://answers.unity.com/themes/thub/images/avi.jpg)