Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

540
Views
How to get text input continuously in Unity C#?

I'm going to get text input using TextField and the following is my code:

public Text textInput;

void Update()
{
    if (Input.GetKeyDown(KeyCode.Return)) {
        if (textInput.text == "go") {
            /* do something */
            textInput.text = "";
        }
    }
}

I assigned the Text element under TextField to public Text textInput, and I expected that once a player types "go" and presses enter, the TextField will be cleared and the player be able to type next sentence.

However, after I type "go" and press enter, the text in TextField remained and the focus was out.

How can I clear the TextField maintaining the focus on it?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I think the "problem" is that the input field by default also handles the Return key as the submission command which automatically loses the focus.

The default InputField.lineType is

SingleLine

Only allows 1 line to be entered. Has horizontal scrolling and no word wrap. Pressing enter will submit the InputField.

You could instead set it to MultiLineNewLine

Is a multiline InputField with vertical scrolling and overflow. Pressing the return key will insert a new line character.

So it doesn't automatically call the OnSubmit and lose focus when pressing the Return key.


Using that as an alternative to use Update and GetKeyDown you could use InputField.onValidateInput and do something like

public InputField textInput;

private void Awake ()
{
    textInput.lineType = InputField.LineType.MultiLineNewLine;
    textInput.onValidateInput = OnValidate;
}

private char OnValidate(string text, int charIndex, char addedChar)
{
    if(addedChar == '\n')
    {
        if(text.Equals("go"))
        {
            // Do something
        }

        textInput.text = "";
        return '';
    }

    return addedChar;
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!