Waiting for keypress from user (how to make a co-routine?) C#

Hi,

So after doing some reading and checking out everyone else’s code, I’m still unsure of how a co-routine should be constructed. I noticed many people do this with a while loop. But I have a for loop that I would like to do this with, I need it to stop at the if statement and check to see what the user pressed. Suggestions?

void LetterChecker() {
        for (int i = 0; i < gCharArray.Length; i++) {
            string charHolder = gCharArray*.ToString();*

if (Input.GetKeyDown(charHolder)) {
print(charHolder);

}else {
print(“Wrong Letter”);
}
}
print(“Word Typing Ended”);
}

This is basically one of the easiest way to make use of co-routine written in C#:

IEnumerator YourCoroutineName(){
// Your code that you want here 
// In this case is the for function
}
void YourFunctionName(){
    StartCoroutine("YourCoroutineName");
}

Of how to wait until the user to type then why don’t you try while loop, may be it’s the best and common way to use. Myself don’t have any suggestion for this issue.

When using a coroutine to wait for specific input or actions, I like to do something like this.

	IEnumerator MyCoroutine()
	{
		for (int i = 0; i < gCharArray.Length; i++)
		{
			string charHolder = gCharArray*.ToString();*
  •  	while ( !Input.GetKeyDown(charHolder) )*
    
  •  		yield return null;*
    
  •  	print (charHolder);*
    
  •  }*
    
  •  print("Word Typing Ended");*
    
  • }*

If you must check for the wrong key I would replace the while body with

  • IEnumerator MyCoroutine()*
  • {*
  •  for (int i = 0; i < gCharArray.Length; i++)*
    
  •  {*
    

_ string charHolder = gCharArray*.ToString();*_

* while ( !Input.GetKeyDown(charHolder) )*
* {*
* if ( Input.anyKeyDown )*
* {*
* print(“Wrong Letter”);*
* }*

* yield return null;*
* }*

* print (charHolder);*
* }*
* print(“Word Typing Ended”);*
* }*