How to set a layer with code using collisions?

I am trying to make an object go up or down in the layers when my player enters an area, but it isn’t working. I am creating a 2d game. I am using C#.

This is my code:

{

void OnCollisionEnter (Collision col) 
{
	if (col.gameObject.name == "Player") 
		{
			col.gameObject.layer = 4;	
		}
}
void OnCollisionExit (Collision col)
{
	if (col.gameObject.name == "Player") 
		{
			col.gameObject.layer = 2;
		}
}

}

Unfortunately layers don’t work like that, 2 or 4 won’t tell the program that you want the 2nd or 4th layer in the list. You can either save tha layermasks to variables or you can use this function: Layermask.NameToLayer(“layerName”);