- Home /
Event.current problem
Hi
I'm trying to make a text-based terminal style thing for a class where there is a gui textfield that you type a command into, then hit enter, then it prints stuff. The problem I've got is that once return is hit once, it seems to think that event.current is ALWAYS return. Here's the code:
void OnGUI() {
// for accepting text input-----------------------------------------------------------
Event e = Event.current;
GUI.SetNextControlName ("input");
GUI.FocusControl ("input");
inputText = GUI.TextField (new Rect (10, Screen.height - 50, 200, 20), inputText, 25);
if ((e.keyCode == KeyCode.Return) && (stage % 2 == 0)) {
hasReturned = true;
userResponse = inputText;
}
//------------------------------------------------------------------------------------
// introduction
if ((hasReturned == true) && (stage == 0)) {
hasReturned = false;
GameObject newU0;
loc.Set(loc.x, loc.y, loc.z);
newU0 = Instantiate(UResponse, loc, transform.rotation) as GameObject;
TextMesh tu;
tu = (TextMesh)newU0.GetComponent(typeof(TextMesh));
tu.text = ">" + userResponse;
inputText = "";
stage++;
GameObject newR0;
loc.Set(loc.x, loc.y - 2, loc.z);
newR0 = Instantiate(RResponse, loc, transform.rotation) as GameObject;
TextMesh tr;
tr = (TextMesh)newR0.GetComponent(typeof(TextMesh));
tr.text = ">Hello " + userResponse + ", it's nice to finally get some company.";
stage++;
hasReturned = false;
Debug.Break();
}
// text to come after intro
if ((hasReturned == true) && (stage == 2)) {
hasReturned = false;
GameObject newU1;
loc.Set(loc.x, loc.y - 2, loc.z);
newU1 = Instantiate(UResponse, loc, transform.rotation) as GameObject;
TextMesh tu;
tu = (TextMesh)newU1.GetComponent(typeof(TextMesh));
tu.text = ">" + userResponse;
inputText = "";
stage++;
GameObject newR1;
loc.Set(loc.x, loc.y - 2, loc.z);
newR1 = Instantiate(RResponse, loc, transform.rotation) as GameObject;
TextMesh tr;
tr = (TextMesh)newR1.GetComponent(typeof(TextMesh));
tr.text = ">Hello " + userResponse + ", it's nice to finally get some company";
stage++;
}
}
}
So the problem is that it prompts the user, then if the user enters their name and presses return, it gives the response correctly then immediately prints the next part. It seems that the event.current is always giving "return", so it immediately jumps to the next part and prints it. I've also tried adding a 3rd statement to the condition:
if ((e.type == Event.KeyDown) && (e.keyCode == KeyCode.Return) && (stage % 2 == 0)) {
But that makes it so that nothing happens at all when return is pressed. I've been looking all over for an answer, but no solution has seemed to work. Any help on this would be excellent. Thanks
Answer by ArjunTheMiella · Apr 13, 2014 at 10:18 PM
Okay I solved it. For whatever reason, adding a the type checking statement against Event.KeyDown did not work but checking it against Event.KeyUp did work. All the solutions I found online suggested the former solution, so to anybody with the same problem, try the unlikely solution of Event.KeyUp.
Your answer
![](https://koobas.hobune.stream/wayback/20220613142638im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Check if enter is pressed inside a GUI textfield 6 Answers
key binding 0 Answers
Gui.textfield empty deletes. 0 Answers
Unresponsive to touch events in android. RaycastHit2D not working anymore? 0 Answers