Search Unity

How to handle multiple Input Fields

Discussion in 'Game Design' started by taivasltd, Jul 5, 2019.

  1. taivasltd

    taivasltd

    Joined:
    Mar 27, 2019
    Posts:
    20
    Hi there! Bit new to unity so probably I know 10% of what I am doing atm :)

    As you see in the attached I have 2 Input Fields.

    1. They are part of the SAME canvas (not sure if it's part of the issue)
    2. The one on the left is a normal Input field, the one on the right is one I created with TextMesh Pro.
    3. The one on the left seems to always have focus correctly, the one on the right never gets it (when I click on it, nothing happens)
    4. The one on the left was from a tutorial code, the one on the right I created for a separate purpose. Ideally I want both of them to work independently, but ok switching from one to the other realistically is fine as well
    input.png

    After doing bit of searching I put the following script on the right-hand side input field but doesn't really help:


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using TMPro;
    public class ActivateNotepad : MonoBehaviour
    {
    public void Activate(TMP_InputField notepad)
    {
    notepad.Select();
    notepad.ActivateInputField();
    }

    }


    and added the relevant GameObject to the OnSelect option of TextMeshPro input field.

    Any idea how I can best design this situation? :)
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You shouldn't need any code to make an input field (whether TMPro or classic style) get the focus and operate normally. You can have any number of them on a canvas, and either tab between them or click in any one to give it the focus.

    If that's not happening for you, then something is wonky.
     
  3. Volcanicus

    Volcanicus

    Joined:
    Jan 6, 2018
    Posts:
    169
    On the canvas, are they all in the front?
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Hey good point — it does rather sound like the TMPro field is stuck behind something, and so not receiving clicks.
     
    Joe-Censored likes this.
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Yeah I was also thinking you probably have something invisible sorted above the TMP field. In a canvas, everything is placed in their order in the scene's hierarchy, with the bottom most object sorted to the top of the canvas. So you probably want to put both of those input fields at the bottom of the list in the hierarchy.
     
  6. taivasltd

    taivasltd

    Joined:
    Mar 27, 2019
    Posts:
    20
    Thanks very much guys, I will restructure a bit the UI because I am sure I have it bit messed up (I have split the canvas in panels and then in the panels I have put the elements so something weird must be happening there) but great to know there is no further trick or work required!