Coroutine acting strange

I get this warning:
Cannot use ‘velocity’ on a static body.
Even tho I make it dynamic before applying the velocity.
I only added the second “WaitForSeconds” to try to solve the problem. I call the coroutine by pressing a button. Help?

IEnumerator frezePhysics()
    {
        velocity = rb.velocity;
        rb.bodyType = RigidbodyType2D.Static;
        yield return new WaitForSeconds(0.3f);
        rb.bodyType = RigidbodyType2D.Dynamic;
        yield return new WaitForSeconds(0.1f);
        rb.velocity = velocity;
    }

If somebody sees this, is there a way to execute code in OnTriggerEnter and Exit only once?