Help: How to detect user input in nested Coroutines?
Hi guys, I am having an issue with detecting when a key was pressed inside some nested Coroutines.
What my code does so far is, detecting pairs of GameObjects, then according to the Objects play a specific soundfile. In addition it should then measure the time that passed from when the sound file stopped playing to when the user first hit the "y" or "m" key.The second Coroutine(measuring the time) should wait until the user has pushed either the "y" key or the "m" key and should not be allowed to proceed before an input was made.
Therefore I have 2 nested Coroutines (SelectSound->handelAudio) It's getting pretty frustrating as I can not figure out what the Problem is. Here is the Code:
I have tried to use Input.GetKey but that did not work inside the coroutine. Also
e.type == EventType.KeyDown && e.keyCode == KeyCode.Y did not work. In both cases the while loop did not work and unity crashed.
I also tried to use update() and OnGUI() in combination with the second Coroutine "handleAudio" but that crashed as well.
I am thankful for any tip on how to solves that issue.
What my code does so far is, detecting pairs of GameObjects, then according to the Objects play a specific soundfile. In addition it should then measure the time that passed from when the sound file stopped playing to when the user first hit the "y" or "m" key.The second Coroutine(measuring the time) should wait until the user has pushed either the "y" key or the "m" key and should not be allowed to proceed before an input was made.
Therefore I have 2 nested Coroutines (SelectSound->handelAudio) It's getting pretty frustrating as I can not figure out what the Problem is. Here is the Code:
I have tried to use Input.GetKey but that did not work inside the coroutine. Also
e.type == EventType.KeyDown && e.keyCode == KeyCode.Y did not work. In both cases the while loop did not work and unity crashed.
I also tried to use update() and OnGUI() in combination with the second Coroutine "handleAudio" but that crashed as well.
I am thankful for any tip on how to solves that issue.
//Choosing sound according to the gameObject
IEnumerator selectSound(GameObject object1, GameObject object2)
{
this.nameObject1 = object1.name;
this.nameObject2 = object2.name;
if (nameObject1 == "Bottle" && nameObject2 == "Book") {
audio.clip=audioBottleBook;
yield return StartCoroutine(handleAudio());
}
if (nameObject1=="Bottle" && nameObject2=="Cup")
{
audio.clip=audioBottleCup;
yield return StartCoroutine(handleAudio());
}
if (nameObject1=="Bottle" && nameObject2=="Lamp")
{
audio.clip=audioBottleLamp;
yield return StartCoroutine(handleAudio());
}
Debug.Log ("End select sound");
}
IEnumerator handleAudio()
{
audio.Play ();
yield return new WaitForSeconds(audio.clip.length);
//Save point in time when audio has finished
this.t_0=Time.realtimeSinceStartup;
this.keyPushed = false;
while (keyPushed==false)
{
Event e = Event.current;
if ((e.type == EventType.KeyDown && e.keyCode == KeyCode.Y))
{
Debug.Log ("Current Key pushed :" + this.currentKeyPushed);
keyPushed=true;
}
if ((e.type == EventType.KeyDown && e.keyCode == KeyCode.M))
{
Debug.Log ("Current Key pushed :" + this.currentKeyPushed);
keyPushed=true;
}
yield return null;
}
