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.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269) UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1) 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(EventHandler`1, Object, TouchEventArgs) (at Assets/TouchScript/Scripts/Utils/EventHandlerExtensions.cs:28) TouchScript.TouchManagerInstance:updateEnded(List`1) (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.
Answer by KoenigX3 · Jan 05, 2017 at 03:04 PM
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);
So yes i tried this and I haven't the error anymore but it remove nothing...
Your answer
Follow this Question
Related Questions
Handling Multiple U.I. Input Fields 0 Answers
Certain scripts that are attached does not work after build, scene change and closing unity 0 Answers
How to add more UI elements to Dropdown OptionData except default string and sprite values in Unity? 0 Answers
Simple Camera Switch Using C# 3 Answers
Display list as UI or GUI 0 Answers