0
\$\begingroup\$

I have a list of TMP_InputFields on my canvas like OTP boxes. I made it in such a way that the cursor automatically moves to the next box once the user types something in the current box. I am working on the backspace such that when it's pressed, the current box gets emptied and the previous box gets activated for the user.

I am having issues with getting the currently active TMP_InputFields out of these input fields. By active, I mean the box that currently has the cursor. I have tried looping through the list and checking ".enabled = true" and "isFocused" but both are not doing what I want.

Can anyone guide me through, please

\$\endgroup\$
2
  • \$\begingroup\$ Sorry this should have been a comment If you move cursor to next TMP_InputFields don't you already have reference to that gameobject? \$\endgroup\$
    – Ivan
    Commented Mar 13, 2023 at 8:43
  • \$\begingroup\$ How do you move your cursor you just move it down from the previous position or? \$\endgroup\$
    – Ivan
    Commented Mar 13, 2023 at 8:44

1 Answer 1

0
\$\begingroup\$

The .isFocused is working as it should. Here is a minimal example that prints the current selected InputField.

public TMP_InputField [] inputFields;

void Update() {
    foreach (var input in inputFields) {
        if (input.isFocused) Debug.Log(input.name);
    }
}

If you tested it with a button click, the Inputfield is not anymore focused. If you have a different problem, please add your code as well.

\$\endgroup\$
5
  • \$\begingroup\$ Thanks you..It works now. I was doing exactly the same thing but not inside Update. \$\endgroup\$ Commented Mar 13, 2023 at 13:30
  • \$\begingroup\$ It really depends on where you are doing it. In Start it gets called exactly once. On a button you loose your focus. You would need to share how you called it for us to tell you why it is not working as you think it is \$\endgroup\$
    – Zibelas
    Commented Mar 13, 2023 at 13:37
  • \$\begingroup\$ So, I have a method that works exactly like the backspace on the keyboard. It takes a string and reduces it by one with the Substring method. This was just for a single input field. Getting the active field didn't matter since I only had one at that time. So when I decided to switch to several input fields, I edited this method and forgot that I would need to check for the active field in every frame to use my new logic. When I saw Update in your answer, it was like a veil was lifted from in front of me because I had tried the same thing several times \$\endgroup\$ Commented Mar 13, 2023 at 13:51
  • \$\begingroup\$ There are better ways than to check every update for the active field. The answer is pointing out that the right attribute is working. You could use an event when a new input gets selected, store the current active InputField and just use the stored as current active. Since it won't change every Update, there is no need to check for it every Update \$\endgroup\$
    – Zibelas
    Commented Mar 13, 2023 at 14:02
  • \$\begingroup\$ Great to know there are better ways. I'm still at the early stage of this career I've chosen. Could you please edit your answer to reflect this new suggestion \$\endgroup\$ Commented Mar 13, 2023 at 14:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .