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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

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

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

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

TextArea does not fire KeyDown events 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