InputField remove one character

Hi

I’m working on multitouch apps.
These apps are running one multitouch device under Windows 10
I don’t have any physical keyboard one these device so i need a virtual keyboard.

I tried this solution : http://answers.unity3d.com/answers/1134812/view.html
But the keyboard is really slow and give options to leave my application.

So i start to create my own keyboard.
So i create a UIPanel, and buttons for letters
This is working well
Then i tried to add a backspace button and here start my problem :

I can’t erase a character from the input field
when i press my backspace touch i call a function where i do this :

field.GetComponentInChildren<InputField>().text.Remove(field.GetComponentInChildren<InputField>().text.Length, 1);

field is my inputfield gameObject
But i have this error :

ArgumentOutOfRangeException:
startIndex + count > this.length
Parameter name: count
System.String.Remove (Int32
startIndex, Int32 count) (at
/Users/builduser/buildslave/mono/build/mcs/class/corlib/System/String.cs:1747)
Keyboard.addInput () (at
Assets/Scripts/Keyboard.cs:23)
UnityEngine.Events.InvokableCall.Invoke
(System.Object args) (at
C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:153)
UnityEngine.Events.InvokableCallList.Invoke
(System.Object parameters) (at
C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:630)
UnityEngine.Events.UnityEventBase.Invoke
(System.Object parameters) (at
C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:765)
UnityEngine.Events.UnityEvent.Invoke
() (at
C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53)
UnityEngine.UI.Button.Press () (at
C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35)
UnityEngine.UI.Button.OnPointerClick
(UnityEngine.EventSystems.PointerEventData
eventData) (at
C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44)
UnityEngine.EventSystems.ExecuteEvents.Execute
(IPointerClickHandler handler,
UnityEngine.EventSystems.BaseEventData
eventData) (at
C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler]
(UnityEngine.GameObject target,
UnityEngine.EventSystems.BaseEventData
eventData,
UnityEngine.EventSystems.EventFunction1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269) UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction1)
TouchScript.Behaviors.TouchScriptInputModule:endPointer(PointerEventData)
(at
Assets/TouchScript/Scripts/Behaviors/TouchScriptInputModule.cs:343)
TouchScript.Behaviors.TouchScriptInputModule:processEnded(TouchPoint)
(at
Assets/TouchScript/Scripts/Behaviors/TouchScriptInputModule.cs:463)
TouchScript.Behaviors.TouchScriptInputModule:touchesEndedHandler(Object,
TouchEventArgs) (at
Assets/TouchScript/Scripts/Behaviors/TouchScriptInputModule.cs:579)
TouchScript.Utils.EventHandlerExtensions:InvokeHandleExceptions(EventHandler1, Object, TouchEventArgs) (at Assets/TouchScript/Scripts/Utils/EventHandlerExtensions.cs:28) TouchScript.TouchManagerInstance:updateEnded(List1)
(at
Assets/TouchScript/Scripts/TouchManagerInstance.cs:699)
TouchScript.TouchManagerInstance:updateTouches()
(at
Assets/TouchScript/Scripts/TouchManagerInstance.cs:787)
TouchScript.TouchManagerInstance:Update()
(at
Assets/TouchScript/Scripts/TouchManagerInstance.cs:541)

Then i tried to use a temp string where i remove one character but the string in my inputfield does not change.

The first argument of string.Remove() is the start index. In this situation, the start index equals to the length of the text, which is incorrect. For example, if you have a string of 15 characters, the last character will have an index of ‘14’, that’s why you can not remove the character with the index of ‘15’ (since there is no such character).

The solution is to subtract 1 from the start index:

field.GetComponentInChildren<InputField>().text.Remove(field.GetComponentInChildren<InputField>().text.Length - 1, 1);

This line only creates a new string with the removed character. You have to assign this to the text variable of the InputField (sorry for not mentioning this, my mistake).

field.GetComponentInChildren<InputField>().text = field.GetComponentInChildren<InputField>().text.Remove(field.GetComponentInChildren<InputField>().text.Length - 1, 1);

Ok solved the problem by using a temp var
But it is strange that i cannot remove the character directly in the inputfield no ?
there should be a way to do that without this temp var…
Thx @KoenigX3