How to move player using Coroutine?

I have a player character that has its movements saved into a file. The saved inputs are mainly WASD chars. The file is then read from another script to iterate through the file and receive the inputs (ig. a,d,space). The codes are as below:-

	private void Update()
	{
	StartCoroutine("RelayInputs");
	}

	IEnumerator RelayInputs(){
	readTextFile(Application.dataPath + "/KeycodeList.txt");
	
	yield return new WaitForSeconds (0.5f);
			
		}

	void readTextFile(string file_path)
	{
		StreamReader inp_stm = new StreamReader(file_path);
		
		while(!inp_stm.EndOfStream)
		{
			string inp_ln = inp_stm.ReadLine( );
			// Do Something with the input. 
			string[] splitArray =  ((string) inp_ln).Split(char.Parse(" ")); // split space in string
		for(int i = 0; i < splitArray.Length; i++)
		{
			Debug.Log(splitArray*);*

_ switch (splitArray*)_
_
{_
_
case “a”:_
_ transform.position = new Vector2(110timePressed,0);_
_
break;_
_
case “d”:_
_ transform.position = new Vector2(-1 * 10 * timePressed, 0);_
_
break;_
_
case “space”:_
_ transform.position = new Vector2(0,1 * 400);_
_
break;*_

* }*
* }*
* // Debug.Log (“STRING FROM FILE:” + inp_ln);
_
}_
inp_stm.Close( );
_
}_
timePressed is the duration of how long the key is pressed by the player
I managed to be able to run it but it only displays each character that exists in the file and how many of it is in the file. Image below is the Debug:
_
[66615-codes.png*|66615]*
_*
I wanted to know if there is a way to read the file and allow the player to move based on what the sequence of chars that has been stored in file.

The problem is the way you set up your coroutine. Coroutine don’t automatically repeat every frame, so you need to use some form of iteration inside. The yield statement is then used to break out of the function for a period of time before continuing where it left off.

When you call StartCoroutine, the readTextFile function is running to completion before you ever reach the yield statement.

I’m guessing that you want to pause in between player moves, so the following code which uses readTextFile as your coroutine should work.

 private void Update()
 {
 StartCoroutine(readTextFile(Application.dataPath + "/KeycodeList.txt"));
  }

 IEnumerator readTextFile(string file_path)
 {
     StreamReader inp_stm = new StreamReader(file_path);
     
     while(!inp_stm.EndOfStream)
     {
         string inp_ln = inp_stm.ReadLine( );
         // Do Something with the input. 
         string[] splitArray =  ((string) inp_ln).Split(char.Parse(" ")); // split space in string
     for(int i = 0; i < splitArray.Length; i++)
     {
         Debug.Log(splitArray*);*

switch (splitArray*)*
{
case “a”:
transform.position = new Vector2(110timePressed,0);
break;
case “d”:
transform.position = new Vector2(-1 * 10 * timePressed, 0);
break;
case “space”:
transform.position = new Vector2(0,1 * 400);
break;
}
yield return new WaitForSeconds (0.5f);
}
// Debug.Log (“STRING FROM FILE:” + inp_ln);
}
inp_stm.Close( );
}