How do i use a coroutine to wait for 1 left mouse button click?
So I am trying to execute some code within a coroutine, then have it yield return and wait for left mouse click. Currently i am trying to do this by having another coroutine that waits for the mouse click that is called on a yield return. Something like this:
IEnumerator Example()
{
Debug.Log("Doing x")
yield return StartCoroutine(WaitForLeftClick());
Debug.Log("Doing Y")
}
IEnumerator WaitForLeftClick()
{
while (!Input.GetMouseButtonDown(0))
{
yield return null;
}
Debug.Log("lmb clicked");
}
I only want it to wait for 1 click but this outputs "lmb clicked" twice and in doing so executes the rest of the Example() coroutine twice. Is there a way to amend so it doesn't execute twice, or is there a different better way to do this? Any help would be appreciated, thanks.
Turns out this wasnt the issue. This simplified version did work. In my actual program it wasnt working because it was waiting for left click twice in the same coroutine and i had to put a delay in so it didnt register the same click.
Your answer
Follow this Question
Related Questions
What is the Unity-esque way for a yielding function to return a value to another yielding function? 0 Answers
Problem with coroutine / yield 2 Answers
How to execute a function after another function has finished its execution? 0 Answers
Could not load source 'Coroutines.cs': No source available. 1 Answer
Checking Internet inside a Coroutine 0 Answers