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. Dismiss Notice

TextMesh Pro ActivateInputField not working in OnEnable

Discussion in 'UGUI & TextMesh Pro' started by squarewave, Aug 9, 2018.

  1. squarewave

    squarewave

    Joined:
    Jul 19, 2017
    Posts:
    4
    EDIT: This actually appears to be a bug in TMP_InputField not InputField. Updated repro steps later in the thread.

    I found an old issue (637877) that was supposedly fixed. However, when I call ActivateInputField in OnEnable the InputField does not receive focus. I end up having to set a flag in OnEnable and then call the same code in Update for it to work properly. Super ugly...

    Am I doing something stupid here?

    This doesn't work:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class Console : MonoBehaviour
    5. {
    6.     public GameObject inputField;
    7.  
    8.     private void OnEnable()
    9.     {
    10.         InputField field = inputField.GetComponent<InputField>();
    11.         field.ActivateInputField();
    12.     }
    13. }
    14.  
    This however DOES work:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class Console : MonoBehaviour
    5. {
    6.     public GameObject inputField;
    7.     private bool shouldEnable = false;
    8.  
    9.     private void OnEnable()
    10.     {
    11.         shouldEnable = true;
    12.     }
    13.  
    14.     private void Update()
    15.     {
    16.         if(shouldEnable)
    17.         {
    18.             InputField field = inputField.GetComponent<InputField>();
    19.             field.ActivateInputField();
    20.             shouldEnable = false;
    21.         }
    22.     }
    23. }
    24.  
     
    Last edited: Aug 29, 2018
    Protozoaire likes this.
  2. FernandoHC

    FernandoHC

    Joined:
    Feb 6, 2018
    Posts:
    333
    Don't know about that bug, but you can try 2 things, either try Canvas.Forceupdate before activating it.
    Or if it still persists I've rewrote your code to be Update independent.
    Also, there is no point in referencing inputfield as a gameobjct and then getting component, so I changed that too.
    Code (CSharp):
    1.     public InputField inputField;
    2.  
    3.     private void OnEnable()
    4.     {
    5.         Invoke("ActivateInputfield", 0.01f);
    6.     }
    7.  
    8.     private void ActivateInputfield()
    9.     {
    10.         inputField.ActivateInputField();
    11.     }
     
    DemonFangZ likes this.
  3. squarewave

    squarewave

    Joined:
    Jul 19, 2017
    Posts:
    4
    Still a little ugly to call Invoke, but better than what I had! Thanks for the refactor!

    I'll submit a bug.
     
  4. squarewave

    squarewave

    Joined:
    Jul 19, 2017
    Posts:
    4
    Just got back to this...

    As I was creating a small project to reproduce this issue I realized it only occurs when using a TMP_InputField, not an InputField.

    Code (CSharp):
    1. using TMPro;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class Console : MonoBehaviour
    6. {
    7.     //public InputField inputField;
    8.     public TMP_InputField inputField;
    9.    
    10.     private void OnEnable()
    11.     {      
    12.         inputField.ActivateInputField();
    13.     }
    14. }
    There's a repo here with a project to reproduce the Issue:
    https://github.com/baconhand/InputFieldOnEnableBug

    Repro:
    1. Run the project
    2. Type some sample text to test the input is working
    3. Press the TAB key
    4. Press the TAB key again
    Result: TMP_InputField isn't activated. Exact same code works with InputField.
     
  5. alex_roboto

    alex_roboto

    Joined:
    Nov 11, 2019
    Posts:
    24
    I got this to work by moving the select() into Start(). Of course, this isn't the same as OnEnable but for a quick fix for people who don't actually enable or disable.
     
  6. adrianfrancisco

    adrianfrancisco

    Joined:
    Jul 29, 2021
    Posts:
    14
    I am also having this issue, if the input field is a TMPro TMP_InputField then the functions ActivateInputField() and DeactivateInputField() simply don't work on them when calling it inside an event.
    TMP_InputField.ActivateInputField() inside a function which is listening to an event--> Doesn't work.
    It seems by the answers above that it indeed works in Update() and Start() functions, but not on OnEnable or other UnityEvents like the one I was trying to use. This seems to be a bug to be fixed
     
    Last edited: Jan 12, 2022
  7. Protozoaire

    Protozoaire

    Joined:
    Apr 14, 2015
    Posts:
    57
    Still an issue in 2023, the above workaround code still works :

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. public class Console : MonoBehaviour
    4. {
    5.     public GameObject inputField;
    6.     private bool shouldEnable = false;
    7.     private void OnEnable()
    8.     {
    9.         shouldEnable = true;
    10.     }
    11.     private void Update()
    12.     {
    13.         if(shouldEnable)
    14.         {
    15.             InputField field = inputField.GetComponent<InputField>();
    16.             field.ActivateInputField();
    17.             shouldEnable = false;
    18.         }
    19.     }
    20. }