• 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
Question by ArjunTheMiella · Apr 13, 2014 at 09:57 PM · eventonguitextfieldreturn

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

Comment

People who like this

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

1 Reply

  • Sort: 
avatar image

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.

Comment

People who like this

0 Show 1 · 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 getyour411 · Apr 13, 2014 at 10:30 PM 0
Share

Converted to an Answer

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

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

Trigger 'if' statement while in TextField 2 Answers

Textfiled Event Handler 1 Answer

How does "Input.eatKeyPressOnTextFieldFocus" work on compilation ? 1 Answer

key binding 0 Answers

Android Bug related to drawing TextField after a Box which is only in a Repaint event 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