Avoid duplicated chars with Input.inputString
I'm making a custom text field and I'm capturing the text with Input.inputString, but I have a problem:
When I'm writing something like "This is a test text", sometimes I get "This iss a test text", with the double "ss" (this could be any char).
This couldn't be a problem if this wasn't a sensible text field (sometimes in the game the player need to enter an exact text).
I think that this could be a "bad luck" timing with the FixedUpdate calling twice just in the bad moment (I mean, for example, in the first call of FixedUpdate the inputString would be "this is" and the second call the inputString would be "s a test" making "this iss a test").
Anyway, here is my code for now:
private void FixedUpdate()
{
if (InputText == null)
return;
char lastChar = '\b';
foreach (char c in Input.inputString)
{
if (c == '\b')
{
if (InputText.text.Length > 1)
{
InputText.text = InputText.text.Substring(0, InputText.text.Length - 1);
}
}
else if ((c == '\n') || (c == '\r'))
{
CommandEntered(InputText.text.Substring(1, InputText.text.Length - 1));
InputText.text = ">";
}
else
{
if(c != lastChar)
InputText.text += c;
lastChar = c;
}
}
}
*Please note that I'm not trying to avoid duplicated chars in a word (for example: "Issue"), but I need to avoid the "Bad timing" making accidental duplicated chars (like "iss")
Your answer
Follow this Question
Related Questions
how do i reference to a text objects text in scripting? 2 Answers
How to change the color of my text object? 1 Answer
Help with text to appear only once and not every time the scene is loaded 1 Answer
How to link up instantiated text in list to allow buttons to adjust number shown, c# 1 Answer
Removing an item from Inventory and updating item count on HUD 0 Answers