Changing layers on runtime does not work

Hello!

I am making a simple 2D platformer runner game like Fun Run. I want the player to go through walls for a second when he respawns. To implement this feature, I added an extra layer (GhostPlayer) to the game. Player is normally in Player layer which collides with everything but in GhostPlayer Layer it only collides with the frame and not with the obstacles on the road.

Both layers are working as intended, if I change the layer and then play. However, if I change the layer in runtime using
gameObject.layer = LayerMask.NameToLayer("GhostPlayer");
player keeps colliding to the obstacles even though I can see in the inspector that I successfully changed the layer.
I do not want to disable the collider2D since then player falls down. And I will also use this method to implement a Dash skill to dash forward a distance where player will not collide with obstacles but will be able to get coins or buffs on the way. That said, any other way to implement this is welcome.
Thank you in advance!

All right, I have found my mistake.
I did get just one part of the project from a tutorial(2D movement code), and apparently, that code uses this;

 void Start()
    {
        contactFilter.useTriggers = false;
        contactFilter.SetLayerMask(Physics2D.GetLayerCollisionMask(gameObject.layer));
        contactFilter.useLayerMask = true;
    }

It is really my fault not to check that part before coming here. I am not yet sure how ContactFilter2D works yet but when I change this part

gameObject.layer = LayerMask.NameToLayer("GhostPlayer");

to

    gameObject.layer = LayerMask.NameToLayer("GhostPlayer");
    contactFilter.SetLayerMask(Physics2D.GetLayerCollisionMask(gameObject.layer));
    contactFilter.useLayerMask = true;

It works as intended.