Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Bug Need to press the key twice?

Discussion in 'Input System' started by michaelscott_88, Feb 6, 2024.

  1. michaelscott_88

    michaelscott_88

    Joined:
    Sep 5, 2013
    Posts:
    1
    I'm having an issue where the user needs to press the 'E' key twice to reopen the text input window. The user will close the window with the 'return' key but then has to press 'E' twice to register on key down again. The 'E' appears to be locked in the down state once the window is opened. This can be replicated by adding the following script to a new game object in a new scene.

    Code (CSharp):
    1. public class GUITest : MonoBehaviour {
    2.     private bool showTextInput = false;
    3.     private string text = "";
    4.  
    5.     private void Update() {
    6.         if (Input.GetKeyDown(KeyCode.E))
    7.             showTextInput = true;
    8.     }
    9.  
    10.     private void OnGUI() {
    11.         if (showTextInput) {
    12.             Event e = Event.current;
    13.             if (e.type == EventType.KeyDown && e.keyCode == KeyCode.Return)
    14.                 showTextInput = false;
    15.  
    16.             GUI.FocusControl("textInput");
    17.             GUI.SetNextControlName("textInput");
    18.             GUI.skin.textField.fontSize = 36;
    19.             text = GUI.TextField(new Rect(200, Screen.height - 250, Screen.width - 400, 50), text);
    20.         }
    21.     }
    22. }