Setting an InputField

I have been struggling with this for a while and hoped it was an issue with Unity that would be fixed, I have a feeling it is not.

In my project, I create UI GameObjects in the scene and SetActive(true) when needed and SetActive(false) when no longer needed to be displayed (rather than creating and destroying). This all works fine in the editor and on desktop builds, unless I build to iOS?

public class UI_Options : MonoBehaviour
{
    InputField     userNameValue;

    public void AcceptUI()
    {   
        Globals.USER_NAME = userNameValue.text;
        this.gameObject.SetActive(false);
    }
    public void CancelUI()
    {
        this.gameObject.SetActive(false);
    }
    public void EnableUI()
    {
        this.gameObject.SetActive(true);
   
        HelperScript.SetInputField(userNameValue, Globals.USER_NAME);
    }

    void Awake()
    {
        userNameValue = (GameObject.Find("User_Name")).GetComponent<InputField>();
    }
}

The HelperScript is one I have found here, the general code is :-

public class HelperScript
{
    public static void SetInputField (InputField inputField, string text)
    {
        if (inputField != null)
        {
            Debug.Log("Setting inputField to "+text);
            inputField.textComponent.text = text;
            inputField.MoveTextEnd(true);
            inputField.text = text;    // this causes an error on ios?
        }
    }
}

But, I get an error on iOS with the inputField.text line?

Setting inputField to Flash
(Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineDebug.gen.cpp Line: 56)

ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index
  at System.Collections.Generic.List`1[UnityEngine.UICharInfo].get_Item (Int32 index) [0x00000] in <filename unknown>:0
  at UnityEngine.UI.InputField.SetDrawRangeToContainCaretPosition (UnityEngine.TextGenerator gen, Int32 caretPos, System.Int32& drawStart, System.Int32& drawEnd) [0x00000] in <filename unknown>:0
  at UnityEngine.UI.InputField.UpdateLabel () [0x00000] in <filename unknown>:0
  at (wrapper delegate-invoke) UnityEngine.Events.UnityAction:invoke_void__this__ ()
  at UnityEngine.UI.Graphic.SetVerticesDirty () [0x00000] in <filename unknown>:0
  at UnityEngine.UI.Text.set_text (System.String value) [0x00000] in <filename unknown>:0
  at HelperScript.SetInputField (UnityEngine.UI.InputField inputField, System.String text) [0x00000] in <filename unknown>:0
  at UI_Options.EnableUI () [0x00000] in <filename unknown>:0
  at TitleScript.Update () [0x00000] in <filename unknown>:0
(Filename:  Line: -1)

Does anyone else have this problem, or is it just me?

I am still struggling with this.

If anyone else has this problem, can you let me know so I don’t feel so alone? :wink:

Failing that, how do others handle the setting of imputField when displaying a ui? Perhaps that could help me reengineer my code to work on iOS. The confusing part is these errors only happen on iOS and not on desktop builds.