I'm having some trouble working with this, it's basically a sword that points toward your mouse position, and when you Left-Click, a Trigger Collider is activated and then deactivated, but I can't get it to work.

Here is my code :

void Attack()
    //Activates the Sword Collider
    polygonCollider2D.enabled = true;
    //Debug Line
    Debug.Log("Collider Active");
    polygonCollider2D.enabled = false;

IEnumerator Wait(float seconds)
    yield return new WaitForSeconds(seconds);

I did it this way hoping that the Wait method would give the game enough time to react to an active collider, and using some Debug.Log(). I know that the collider actually activates, but it does not react to other colliders.

I keep wondering if this is the proper way of working with this. If it's not, do let me know.


1 Answer 1


Add the polygonCollider2D.enabled = false line after the yield return in the coroutine instead. In your code this line is executed immediately because a coroutine is executed over multiple frames based on what it yields. Whatever is after the yield return statement will be executed after seconds seconds, but while the coroutine is waiting control is returned to the coroutine caller so that it can continue execution and isn't blocked. So this statement is executed immediately, since while coroutine Wait waits, control is returned to Attack and it executes the last statement.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .