Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Button linked to a boolean input field.

Discussion in '2D' started by meikopfaffmann03, Nov 23, 2019.

  1. meikopfaffmann03

    meikopfaffmann03

    Joined:
    Nov 14, 2019
    Posts:
    8
    Hello.
    I want that when I press a number or comma or dot on my keyboard, it automatically (without clicking the input field) enters a into a input field, then if I press "Enter" ( or a specific key), the input field should empty out and the input should be stored in a variable which I can process further or compare against other variables.
    How can I accomplish this task, and furthermore how can I use this variable in another script of another GameObject?
    Any help would be appreciated, thank you all in advance!
     
    Last edited: Nov 23, 2019
  2. Primoz56

    Primoz56

    Joined:
    May 9, 2018
    Posts:
    369
    You can check for keystrokes in an Update() of any object. I wouldn't necessarily recommend this as it's dirty code, but hey whatever works for you :)
    1. void Update()
    2. {
    3. if (Input.GetKeyDown("f"))
     
    MisterSkitz likes this.
  3. meikopfaffmann03

    meikopfaffmann03

    Joined:
    Nov 14, 2019
    Posts:
    8
    That isn't exactly what I asked for, I know how to detect key presses. Thank you anyways.
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Last edited: Nov 24, 2019
  5. meikopfaffmann03

    meikopfaffmann03

    Joined:
    Nov 14, 2019
    Posts:
    8
    I am very new to this forum, and I didn't see this section. Thank you, I will post correctly in the future.
     
    Last edited: Nov 24, 2019
  6. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @meikopfaffmann03

    This is pretty much what should work I think:
    Code (CSharp):
    1. public class GrabInputFocus : MonoBehaviour
    2. {
    3.     [SerializeField] InputField inputField;
    4.  
    5.     void Update()
    6.     {
    7.         if (Input.GetKeyDown(KeyCode.Comma))
    8.         {
    9.             Debug.Log("Grab focus");
    10.             inputField.Select();
    11.             inputField.ActivateInputField();
    12.         }
    13.     }
    14. }
     
    Last edited: Nov 24, 2019
    MisterSkitz likes this.
  7. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    So you're trying to achieve autofocus and you want the field('s) to clear after entering the data?
    I blieve autofocus can happen with .Select() so you'd put your variable that stores your text field, I'll call it textField on it like this:

    Code (CSharp):
    1. textField.Select();
    And to clear it you only need to do this:

    Code (CSharp):
    1. textField.Clear();
    Now to press any key and it auto-focus on the field, well, you'll need to code it to do so... You will need to check for literally every single key press within the Update().

    Code (CSharp):
    1. void Update()
    2. {
    3. if(Input.GetKeyDown(KeyCode.A)
    4. {
    5. textField.Select();
    6. textField.text = "A";
    7. }
    8. }
    And you will need to do this for every possible key entry.

    Edit:

    Keeping in mind that you would need to concatenate each new letter you enter using textField.text =

    You'll end up having to create an array and store all previously entered chars, and remove deleted, etc... The code I provided will delete the field and place ONLY the new char in it. Just thought you should know.
     
    Last edited: Nov 26, 2019