StartCoroutine still runs 1 more second after using yield break

I tried using the StartCoroutine to make my pet smile for 1 second when clicked, but after 1 second more, the clicked bool returns several false value for 1 second more. I put yield break after the clicked is returned a false value to break it immediately but still it runs for 1 second more. How do i break it instantly?

IEnumerator PetTouch()
    {
        if (clicked)
        {
            emoteMats[1] = emoteMattouched;
            this.GetComponent<Renderer>().materials = emoteMats;
            yield return new WaitForSeconds(1);
            clicked = false;
            Debug.Log(clicked);
            yield break;
        }
    }

From what i can see, Whats happening here is:

If you observe closely the mouse click is registered on each and every single frame. For example if you hold the mouse click down for a second and you game is running on 60 FPS then total number of clicks detected will be 60. Keep this in mind when you are making a call to your Enumerator. For every Fame your Co-routine will be called which in turn will result and extra second of output (in your case).

so if you are using Input.GetMouseButton(0), please don’t, instead use Input.GetMouseButtonDown(0). It will solve your problem for sure.

:slight_smile: :slight_smile: