char not equal to first char from string

Hi

I have trouble checking if a char from my Input.inputString is != my array string…
Even if i try debugging and it shows the correct word, my code still executes another if statement which should not be true.

        stringToEdit = Input.inputString;

        if (Input.anyKey && stringToEdit.Contains(indexName[0].ToString()))
        {
            Debug.Log("Running");
            checker = true;

        	
        }
        else if (Input.anyKey && !stringToEdit.Contains(indexName[0].ToString()))
        {
            checker = false;
        }

Why does it still go through my second if?

After a lot of comments, the final answer :stuck_out_tongue:

Basically, the problem for why the code runs the else if is because Update is called every frame. By default you run your game on 60 frames per second. Which means that Update is called 60 times per second (Depends on your settings, but the default is 60)

  • Input.anyKey returns true whenever any key is hold this frame.
  • Input.inputString returns the keys that are hold down this frame But were not last frame.

The difference is that if you hold down a key for exactly one second, 1 time Input.inputString will give you the value of the key you pressed and 60 times Input.anyKey is true.

So for why it goes through your second if, when you press the key, it will go through the first if, but because the game runs at 60 frames per second, and you are not able to release the key before the next frame, the next frame Input.anyKey is still true (Since you still hold the key) but inputString won’t contain the key, as last frame you also pressed that same key.

The piece of code describing how I would check whether the user typed the whole word completely are in the comments (because they are not the actual answer to the question)