The method is executed twice when the button is pressed

void Update()
{
        if (Input.GetButtonDown("Use"))
        {
            StartCoroutine(Interaction(true));
        }
}
public IEnumerator Interaction(bool state)
    {
        if(state == true)
        {
            Check = Physics2D.Raycast(new Vector2(transform.position.x, transform.position.y), RayDirection, RayLength, Mask);
            if (Check.collider != null)
            {
                if (Check.collider.tag == "Dialogue")
                {
                    state = false;
                    yield return new WaitForSeconds(0.5f);
                    Debug.Log("is work");
                }
            }
        }
    }

The console displays the message twice “is work”;
Please tell me what is the error? I need the message to be displayed only once.

Make sure you only have the upper code part on one script on one gameobject. Otherwise, they both will start a coroutine at the same time and thus two of them will be executed and the message will appear twice.
GetButtonDown should only be true for one frame and your coroutine does not contain loops, so that would be my guess.

3 Likes

Probably two callers or two scripts, Debug.Log to see the stacktrace

2 Likes

Thank you for your answers!